views:

23

answers:

1

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

+3  A: 

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');
Nick Craver
I didn't know this was possible, thanks!
Tom
@Tom - welcome :)
Nick Craver
@Tom: See the definition of jQuery: http://api.jquery.com/jQuery/
Felix Kling
Thanks. By the way, would something like $(myNode).(":parent") be possible?
Tom
@Tom - Yeah of course, `$(myNode).parent()` :) See the other tree traversal (moving around) functions available here: http://api.jquery.com/category/traversing/tree-traversal/
Nick Craver