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?
To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.
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.
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 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.
$('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/
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' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With