Tutorial :How determine child ordinal position? Need: $(“#parent”).ordinalChild('#some-child') => 3 (say)



Question:

Because Jquery UI tab doesn't support passing id selector to remove() tab method, and no method exists to map id to index, I have to painfully "jump hoops" to determine the index of the tab I wish to remove.

I just searched the documentation for JQuery and wasn't able to find a utility method to return an integer of the ordinal position of some child under a parent. I think I want the functional opposite of the

.eq()  

traversing method (determine index, not select by index).

Does something like this exist?:

$("#parent").ordinalChild('#some-child')  

This would return 0 if "some-child" is the first child, 1 for the second child, etc.

Or how do I go about obtaining this without writing a loop and counting?


Solution:1

See index

If a selector string is passed as an argument, .index() returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector. If the element is not found, .index() will return -1.

$('#parent').children().index('#some-child');  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »