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);

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];
Sign up to request clarification or add additional context in comments.

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);

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);

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);

Comments

Your Answer

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.