jquery - Find element index by its text -


<div id='xlink'>mc</div> 

js

$('#next').click(function() { var xlink = $('#xlink').html();  var links = [ 'qd', 'mc', 'vr', 'lm', 'ms', 'kl', 'yu', 'an' ];  foreach(links, element) {      if (element.value==xlink){y = element.index};          alert (y);  }); 

what need:

foreach element in links array, if element value equal xlink value y = index of element. in case should 1.

get elements text use key, , in array indexof, or more cross browser jquery's $.inarray, , return index

$('#next').on('click', function() {     var links = [ 'qd', 'mc', 'vr', 'lm', 'ms', 'kl', 'yu', 'an' ];     var key   = $.trim( $('#xlink').text() );     var y     = $.inarray(key, links);      alert(y); }); 

fiddle


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -