Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of document.getElementbyId in JQuery? [duplicate]

map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

But this does not work:

map = new google.maps.Map($('#map_canvas'), mapOptions);

I am looking for something like...

$('#map_canvas').toElementBlahblah?
like image 544
TIMEX Avatar asked Feb 23 '13 01:02

TIMEX


1 Answers

.get(index)

$('#map_canvas').get(0)
$('#map_canvas')[0]

Though, document.getElementById obviously has better performance - it is the method used internally by the jQuery core when querying the DOM for a single ID selector.

So then you're building a jQuery object just to discard it afterwards. Up to you whether to save some bytes in the bandwidth or microseconds in execution time.

Not much difference, honestly. I use the jQuery version when performance is not concerned and I'm lazy to type document.getElementById, though vanilla JS is a bit more logical in this case.

like image 77
Fabrício Matté Avatar answered Nov 02 '22 23:11

Fabrício Matté