Monday, April 6, 2009

Convert jQuery object to DOM object

jQuery object is a wrap of the DOM object, but itself is not a DOM object. If you want to get the DOM object from the jQuery object, you should use get() method.

For example, if the jQuery object includes only one DOM object, you can use

$("div").get(0)

or

$("div")[0]

However, if you have multiple DOM objects, you can use

$("div").get()

11 comments:

  1. Short and usefull. Thanks

    This commenting method REALLY sucks...

    ReplyDelete
  2. Now after couple of times when I was trying to comment your post(FINALLY!) I have to say that this commenting method REALLY REALLY SUCKS...

    If You want any coments You'd better change it to something NORMAL. I had to try 4 times before posting my previous comment and normal people aren't so pacient.

    Cheers:)

    ReplyDelete
  3. Thanks a lot, it helped me :)

    ReplyDelete
  4. How do you go the other way around? I have a DOM pointer and I want to use jquery functions on it.

    ReplyDelete
  5. I need to be able to use jquery functions on a DOM pointer/object too.

    ReplyDelete
  6. @mslocum and @Daniel: That's easy! Just wrap it in $() -- for instance, say, you have a DOM object created like this:

    domObject = document.getElementById('mydiv');

    you can turn it into a jQuery object by going like this:

    $(domObject)

    ReplyDelete