Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have an array and a div:
var urls = ["google", "yahoo", "facebook"]; <div class="yahoo"></div>
Is it possible to get the array index of whichever element is loaded as the div's class?
I have tried:
alert(urls.index());
and my page breaks.
You can try $.inArray utility function.
$.inArray
var urls = ["google", "yahoo", "facebook"]; var cls = $('div').attr('class'); var ind = $.inArray(cls, urls);
Add a comment
var urls = ["google", "yahoo", "facebook"]; var pos = getPosition(urls, 'yahoo'); //returns 1 function getPosition(arrayName,arrayItem) { for(var i=0;i<arrayName.length;i++){ if(arrayName[i]==arrayItem) return i; } }
FIDDLE
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.