TheCarver
TheCarver

Reputation: 19713

Selecting an input by specific value with jQuery 1.9+

Bit confused here.

I have a few form fields filled out by a user and I basically need to select the fields that have a specific value and change the color to red, as a validation assist.

This is how I thought it would be:

$('input[value="www.mydomain.com"]').css('color','red');
// or with data from script
$('input[value="'+url+'"]').css('color','red');

But this hasn't worked, so I did some reading and found out that because I'm using jQuery 1.9+, this method does not work as it did before this release. So then I found this, after reading a few SO questions:

$("input").filter(function () {
    return this.value === "www.mydomain.com";
});

But not sure how this works and how to use it my case. Is this the right method for jQuery 1.9+ and how can I get it to work in the same way that I had before, by changing the CSS etc?

Upvotes: 0

Views: 40

Answers (1)

tymeJV
tymeJV

Reputation: 104775

You're right in using the filter function, this will return an array of elements matching the criteria (in this case, those whose value is www.mydomain.com), so simply chain a .css call and you're good!

$("input").filter(function () {
    return this.value === "www.mydomain.com";
}).css('color','red');

Upvotes: 2

Related Questions