views:

967

answers:

3

Can I use comparison statements within a jQuery selector method?
eg.
I have a list of divs generated by php that all use the same CSS class but have value attributes of 1, 2, 3 etc. I also have a text input field with an id. This field can accept numbers only. I would like to select the div (from the long list) that has a value attribute that matches the value put into the text input box.
Can I write something like this:
$( '$(".someClass").val() == $("input#someId").val()' )

???

+5  A: 
$('.someClass').filter(function() {
    return $(this).attr('value') == $('input#someId').val() ;
 })
Marwan Aouida
+4  A: 

Take a look at the CSS3 selectors page: http://www.w3.org/TR/css3-selectors/.

Recommended selector: E[foo="bar"], as in '.class[value="'+$('#inputId').val()+'"]'.

Note: not tested.

Flavius Stef
+1  A: 

it's just a standard jquery selector: attributeEquals

<html>
<head>
 <title>Just a test</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
 <script type="text/javascript">
 $(function() {
  $('#txtValue').change(function(){
   $('.selectMe').css('color', 'black');
   $('.selectMe[value= '+$(this).val()+']').css('color', 'red');
  });  
 });
 </script>
</head>
<body>
 <input type="text" id="txtValue" value=""/>
 <div class="selectMe" value="1">one</div>
 <div class="selectMe" value="2">two</div>
 <div class="selectMe" value="3">three</div>
</body>
</html>
Andrea Balducci