javascript - Identity selectors in jQuery returning arrays -
suppose have div tag this:
<div id="group-dialog" class="modal-dialog"> now want grab jquery object (in case can run .dialog()).
when try this:
var gdialog = $('#group-dialog'); i array (!!).
why getting array? isn't point of having id attribute there's one? can see getting multiple p's or .my-css-thing ...
next question:
i have array 1 object in want access jquery object.
when this:
$(gdialog[0]) and pull in f12, still have array!! thought de-referenced array picking first element.
this doesn't seem either:
var gdialog = $('#group-dialog:first'); this basic, run problem lot. seems used lot simpler!
what best way access dom element jquery object?
answer 1
 jquery selectors return arrays.
 selection id attribute particular use case , ideally result should unique. however, there nothing preventing having  duplicated ids in html document (although bad practice).
answer 2
 following code first element dom object:
var gdialog = $('#group-dialog')[0]; note: may want check size of return array first.
as far know, there no way transform dom element jquery object. standard use case directly used $('#group-dialog') , asume found , unique.
Comments
Post a Comment