2

requirement, want to find the input fields that have been entered specific

html,

<ul class="test-list">
<li>
   <input class="test-input">
</li>
<li>
   <input class="test-input">
</li>
<li>
   <input class="test-input">
</li>
</ul>

example,

if the user entered "test1", "test2", "test3" in each input field

i tried javascript,

$('.test-list').find('.test-input [value="test1"]')

and i expect getting the input field that have entered test1, but it returns 0 array :(

i tried filter() and witht his it works, but i want to find it directly like the first one.

1
  • var value = $('test-list').find('.test-input').attr('value'); Commented Jun 18, 2014 at 10:52

5 Answers 5

4

test-input is classname and not tagname. also you need to remove the space in name-value selector and .test-input. You need to use:

$('.test-list').find('.test-input[value="test1"]');

you can also narrow down selector to:

$('.test-list [value=test1]');

Working Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't required $('.test-list') ?
@Canna: did you removed the space in between selector.
1
  • Your code doesn't work because you are missing a dot
  • Use find() to descend into child nodes and a [value="x"] selector

Solution:

$('.test-list').find('.test-input[value="test1"]');

Resources:

Comments

1

doing [value="test1"] is not rational solution.
what if an element has no "value" attribute?
so I'll do it like this:

$('.test-list').find('.test-input').each(function(){
    var $this = $(this);
    if($this.val() == 'test1') {}
});

Comments

1

try

you missed .class selecor

$('.test-list').find('.test-input [value="test1"]')

DEMO

Comments

0
$('.test-list').find('.test-input[value="test1"]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.