0

I'm trying to recreate the following code with jQuery:

 var node = document.createElement("div");
 node.innerHTML = "DynaColumn";
 node.className = "ui-state-default ui-corner-all";
 return node;

this is the code I'm trying to get to work:

return $("<div class='ui-state-default ui-corner-all'>DynaColumn</div>");

The first seems to work fine. I'm not sure why the second isn't working:

The object that will be using this return value is calling it as so:

something.appendChild(theNode);
Andy E
346k86 gold badges482 silver badges452 bronze badges
asked Feb 4, 2010 at 20:44

4 Answers 4

3

appendChild is a DOM method, so needs to be given a DOM Node, not a jQuery wrapper (as returned by $(...) and most other jQuery methods).

Quick way to get the wrapped node out of a jQuery wrapper that contains exactly one element:

return $('<div class="ui-state-default ui-corner-all">DynaColumn</div>')[0];
answered Feb 4, 2010 at 20:47

1 Comment

Thanks, this was exactly what I was looking for. The jQuery wrapper concept is alot more clear to me now.
1

Remember that jQuery objects are wrappers around DOM objects. To get the dom objects out of a jQuery object, use the get() function.

return $('<div class="ui-state-default ui-corner-all">DynaColumn</div>').get(0);
answered Feb 4, 2010 at 20:49

Comments

1

If something is a bare DOM element, then you need:

something.appendChild(theNode[0]);

If something is a jQuery object, then you need:

something.append(theNode);
answered Feb 4, 2010 at 20:50

Comments

0
var $div = $(document.createElement("div"));
$div.html("DynaColumn");
$div.addClass("ui-state-default").addClass("ui-corner-all");
return $div; // or $(something).append($div);
answered Feb 4, 2010 at 20:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.