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
  • 51,276
  • 73
  • 200
  • 335
  • 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
  • 24,096
  • 8
  • 59
  • 61
2

Try:

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

It should be equiv to:

document.getElementById('file-uploader')
chown
  • 51,908
  • 16
  • 134
  • 170
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
  • 13,019
  • 4
  • 43
  • 64