Idham Choudry
Idham Choudry

Reputation: 629

Check if input value exist from unique input name

I have an html input text like this :

<input type="text" name="address[5][firstname]">

i want to check if the value is exist only by input which contain a specific string like firstname. Here's what i've done so far:

if($( "input[name*='[firstname]']" ).length < 1){
   console.log('in');
}

if($( "input[name*='[firstname]']" ).length < 1){
   console.log('in');
}

Upvotes: 3

Views: 411

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 121998

i want to check if the value is exist only by input which contain a specific string like firstname

Your selector is good but your if condition is wrong. You are checking < 1 but there is already an element matched and length is 1, hence the condition not satisfied. Change it to > 0

if($( "input[name*='[firstname]']" ).length > 0){
  alert('in');
}

https://jsfiddle.net/sureshatta/tuwdzgue/

Upvotes: 3

Related Questions