Something I didn’t know about element selectors

I didn’t realise that element functions can only be called on elements that have been selected using the same library.

I used to think that, since jQuery is essentially Javascript, that I would be able to use native Javascript functions on jQuery objects, but obviously not the other way round. But this is not the case.

For example, I cannot use the native Javascript className function on a jQuery element $('div'). I would have to select this element using native selectors like querySelector or getElementById.

Posted Friday, April 5th, 2013 under BBC.

One comment so far

  1. You can’t interact with native JS on the jQuery object itself, but it should work if you do this: $(‘div’)[0].whateverFunction

Leave a Reply