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.

Canna
  • 3,614
  • 5
  • 28
  • 33

5 Answers5

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

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

try

you missed .class selecor

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

DEMO

Balachandran
  • 9,567
  • 1
  • 16
  • 26
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:

martynas
  • 12,120
  • 3
  • 55
  • 60
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') {}
});
num8er
  • 18,604
  • 3
  • 43
  • 57
0
$('.test-list').find('.test-input[value="test1"]')
RGS
  • 5,131
  • 6
  • 37
  • 65