Code has been added to clipboard!

Learn to Use jQuery .wrap(), .wrapAll() and .wrapInner() Methods

Reading time 2 min
Published Jul 3, 2019
Updated Sep 27, 2019

jQuery wrap: Main Tips

  • The .wrap() jQuery function places the specified HTML elements around every selected element.
  • jQuery .wrapAll() puts them around the whole set of matched elements.
  • To place the specified elements around the content of each selected element, use jQuery .wrapInner().

Using .wrap()

The jQuery .wrap() element function inserts the specified HTML content or jQuery object around each matched element. To undo this, use the .unwrap() method.

The following example wraps content around a <h2> element:

Note: jQuery .wrap() can be used to return the unmodified set of HTML elements to use in chaining.

The .wrap() jQuery syntax includes a wrapElement parameter, indicating the content to add:

$(selector).wrap(wrapElement);

Alternatively, you can include a function as the parameter. It specifies the callback function, delivering the content to wrap around selected HTML elements:

$(selector).wrap(function);

Learn to Apply .wrapAll()

Using jQuery .wrapAll() places the specified elements around the whole set of matched elements. See an example below:

Example
$("button").click(() => {
    $("p").wrapAll("<div class=\"main\"></div>");
});

As previously, you have to indicate the content to add in the wrapElement parameter:

$(selector).wrapAll(wrapElement);

Again, you can also add a function, returning the content to wrap:

$(selector).wrapAll(function);

.wrapInner() Explained

Now, jQuery .wrapInner() places the specified elements around the content of every matched element.

jQuery .wrapInner() also accepts a wrapElement parameter, specifying the elements to wrap. It can also be defined in a function, returning the HTML content or JavaScript object to put around elements' content:

$(selector).wrapInner(wrapElement);

$(selector).wrapInner(function);

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX