3

Possible Duplicate:
How to get a DOM Element from a JQuery Selector

I have a JavaScript library that takes one of parameters as

element: document.getElementById('file-uploader')

And it works well, though I try to use jQuery instead and error happens then.

element: $('#file-uploader')

I suppose these return different objects so how can I make it with jQuery but return an object of the same kind as if it were returned by getElementById method?

Community
  • 1
  • 1
Sergei Basharov
  • 47,526
  • 63
  • 186
  • 320
  • 1
    There are at least 2 same questions answered already: http://stackoverflow.com/questions/2316199/jquery-get-dom-node http://stackoverflow.com/questions/1677880/how-to-get-a-dom-element-from-a-jquery-selector – Sotomajor Sep 23 '11 at 08:18

3 Answers3

5

Try -

$('#file-uploader')[0] //or $('#file-uploader').get(0)

This will return the 'naked' JavaScript DOM object, the same as

document.getElementById('file-uploader')

would return. The example above will only return the first element of the matched set, but in a situation where you're searching by id that should be fine.

ipr101
  • 23,772
  • 7
  • 57
  • 61
2

Try:

$('#file-uploader')[0]

It should be equiv to:

document.getElementById('file-uploader')
chown
  • 50,544
  • 16
  • 131
  • 169
2

you have to use either [0] or .get(0) to return the dom object instead of the jquery:

$('#file-uploader')[0];

or

$('#file-uploader').get(0);
rtpHarry
  • 12,690
  • 4
  • 43
  • 61