Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an input by value with jQuery 1.4.3 and higher

Tags:

jquery

With jQuery 1.4.2 and many previous version it is possible to select inputs like this:

$('input[value=test]') 

But with 1.4.3 and higher this selector doesn't work =( Is there any way to select by value in newer versions of jQuery?

like image 873
H1D Avatar asked Jun 22 '11 12:06

H1D


People also ask

How can I get input value in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

How do I set the value of a element in jQuery?

JQuery val() method: This method return/set the value attribute of selected elements. If we use this method to return value, it will return the value of the FIRST selected element. If we use this method to set value, it will set one or more than one value attribute for set of selected elements.

How do I select something in jQuery?

The select() method is an inbuilt method in jQuery which is used when some letters or words are selected (or marked) in a text area or a text field. Syntax: $(selector). select(function);

How can I select an element by name with jQuery?

How to select an element by name with jQuery? The JavaScript getElementsByName() method can be used to select the required element and this can be passed to a jQuery function to use it further as a jQuery object.


2 Answers

$('input[value="test"]') Should work fine...

EDIT

You could try using if statements, for example:

$("input").keyup(function(e) {      if (this.value == 'foo'){         $(this).css('backgroundColor','red');     }     else if (this.value == 'bar'){         $(this).css('backgroundColor','green');     }     else $(this).css('backgroundColor','white'); }); 

http://jsfiddle.net/ajthomascouk/HrXVS/

like image 140
Alex Avatar answered Oct 01 '22 06:10

Alex


As far as I can see, in newer jQuery versions, you can select INPUT elements by their "value" attribute, but not by the current .value property.

When a value is changed by calling .val(), changing the native JS .value or also by direct user input, the HTML attribute is not changed. In former versions, selectors referred to the current value, while now they correctly refer to the HTML attribute.

You can select however the current value with this code:

$('input').filter(function() { return this.value == 'test' }); 
like image 42
filip Avatar answered Oct 01 '22 06:10

filip