I know that when we have an element with id=’someId’, we can access it with Jquery like this:
$('#someId')
But sometimes when we have a variable:
var x;
We use just x or $(x).
When do we use $(x) instead of x? When do we use $($(x)) instead of x?
$(x)creates a jQuery object containingx(which should be a DOM node) or the elements contained inx(if it’s an array or jQuery object).You use
xif you just want the plain DOM object (assumingxis one), e.g.x.idto get the element’s ID as there is no need to create a jQuery object for that – it would be even more to write:$(x).prop('id').You never use
$($(x))! There is just no reason to do that ever. While it works it is just like$(x)except the fact that you first create a jQuery object and then put the contents of that jQuery object into a new jQuery object.If you need the other way (jQuery object => DOM object) there are quite a few ways.
y[0]is the easiest way to get the first element; usey.get()if you want an array with all elements contained in the jQuery objecty.