Bart Blarft
Bart Blarft

Reputation: 45

index for name input

<input type="text" name="test[ooo][]">
<input type="text" name="test[bbb][]">
<input type="text" name="test[ccc][]">
<input type="text" name="test[vvv][]">
​
$('input').each(function(){
  console.log($(this).attr('name').index('bbb'));
})​

Why this not working? How can i check index for attribute name from input?

http://jsfiddle.net/UA2BN/

I don't must use index(). I can use other function, but how can i check this?

Upvotes: 1

Views: 158

Answers (3)

Rachid O
Rachid O

Reputation: 14002

try this code:

inputs= document.getElementsByTagName("input");
for(i=0;i<inputs.length;i++)
   alert(i);​

Upvotes: 1

Krycke
Krycke

Reputation: 3186

This doesn't work because the jQuery selects all for input elements, and loop through them, interpreting the names as name-strings. In jQuery's mind this are not arrays at all.

Off course you can always select a specific input by selecting a substring in its name:

console.log( $('input[name*="[bbb]"]') );​

this will select an input with it's name containing the substring [bbb].

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102763

I think you're looking for indexOf.

console.log($(this).attr('name').indexOf('bbb'));

-1

5

-1

-1

http://jsfiddle.net/5uVd9/

Upvotes: 2

Related Questions