Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#14131 closed bug (notabug)

JQuery warp function is not working properly

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:


Please review my code I create two scripts: native JavaScript and using JQuery wrap function. I would like to report the wrap function is not working properly at least before and after API are not working:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	<div id="native" style="background-color:green"></div>
	<div id="jquery" style="background-color:red"></div>

var container1 = document.getElementById("native");

//create element
var div = document.createElement("div");
div.innerHTML = "HELLO WORLD";

var p = document.createElement("p");
p.innerHTML = "LAST";

var span = document.createElement("span");
span.style.color = "blue";
div.parentNode.insertBefore(span, div);

var p = document.createElement("p");
p.innerHTML = "BEFORE";
span.parentNode.insertBefore(p, span);

var p = document.createElement("p");
p.innerHTML = "AFTER";
span.parentNode.insertBefore(p, span.nextSibling);



var container2 = $("#jquery");

//create element
var div = $("<div></div>");
div.html("HELLO WORLD");

var p = $("<p></p>");

var span = $("<span></span>");
span.css("color",  "blue");

//span = div;

var p = $("<p></p>");

var p = $("<p></p>");



Change History (4)

comment:2 Changed 10 years ago by timmywil

Resolution: notabug
Status: newclosed

We have to clone the element with which the collection is wrapped, which means a new element will be created based on the element passed to wrap. This is to provide a mechanism to conveniently wrap more than one element at a time with the same html. It also avoids unexpected behavior when wrapping with existing elements. Below is one way to get the new span element:


comment:3 Changed 10 years ago by anonymous

HI timmywil, Thanks for your time, but for me your workaround looks very strange

I have a div component (input/select etc)

var div = $("<div></div>");

now I am creating a spam wrapper and apply for the div: <span><div></div></span>

var span = $("<span></span>"); div.wrap(span);

Why should I ask div where is my span if I already have a span variable????

FIX Get the correct span span = div.parent();

before var p = $("<p></p>"); p.html("BEFORE"); span.before(p);

comment:4 Changed 10 years ago by dmethvin

This isn't a bug report, it's a request for jQuery programming help. Wrapping a block element in an inline element doesn't make much sense anyway. Ask for help on a forum.

Note: See TracTickets for help on using tickets.