Tom
Tom

Reputation: 8127

Use html node from Javascript variable with jQuery?

I would like to use the attr() method on a html node, which I have a reference to in a normal variable in Javascript.

Now, the problem is that this node is not returned by a jQuery selector, but with normal Javascript: var myNode = window.getSelection().getRangeAt(0).commonAncestorContainer;

Now, I would like to do something like:

jQuery.attr(myNode, "style");

Obviousely, this does not work. Is there any way to use such jQuery method on a non-jQuery variable (a normal node)?

Upvotes: 2

Views: 507

Answers (1)

Nick Craver
Nick Craver

Reputation: 630429

range.commonAncestorContainer returns a DOM element just wrap it as a jQuery object, like this:

$(myNode)

For example:

var style = $(myNode).attr('style');
//or set it:
$(myNode).css('color', 'red');

Upvotes: 3

Related Questions