JQuery: getting the nth position of an object

I have a div containing checkboxes. I also have a function that takes a parameter from a jquery object. It looks like this:

function analyze_checkbox(cb) { ... }

Given this, is there an easy way to find out the child position of the cb parameter ? I was thinking of something like this:

cb.parent().index(cb)

What is the right way to do this?

Thank,

Erwin

+3
source share
2 answers

Quote from . index ()

If no argument is passed to .index (), the return value is an integer indicating the position of the first element in the jQuery object relative to its native Elements .

So in your case cb.index();should do it

http://www.jsfiddle.net/qQzte/

, 0

+3

, Gaby, , cb.index() , . , , , index. , :

<div id='thediv'>
    <label><input type='checkbox' value='1' name='one'>One</label>
    <label><input type='checkbox' value='2' name='two'>Two</label>
    <label><input type='checkbox' value='3' name='three'>Three</label>
    <label><input type='checkbox' value='4' name='four'>Four</label>
</div>

... cb () $('#thediv input[type=checkbox][name=three]'), cb.index() ). , .

. , label : cb.parent().index() ( ). .

, : , , jQuery, cb . , , :

// The selector choose *exactly* the set of things we want to
// look at, and then `index` looks for `cb` within that list
var index = $('#thediv input[type=checkbox]').index(cb);

+1

Source: https://habr.com/ru/post/1769296/


All Articles