0

I want to change the color of any input on the page with a value of "--Select--". The following code works on blur:

setTimeout(() => { $(document).ready(function () { $('input').blur(function() { if ($(this).val().indexOf('Select') !== -1) { $(this).css('color', '#6D6E71'); } }); }); }, 500); 

But since I want to change color of any input with the value "--Select--" on page load, I changed the code to:

setTimeout(() => { $(document).ready(function () { if ($('input').val().indexOf('Select') !== -1) { $(this).css('color', '#6D6E71'); } }); }, 500); 

This code doesn't work. Why not? How should I change it so it will work? Any help will be appreciated.

1 Answer 1

1

You're not checking all inputs in the $('input') collection. $('input').val() will only return the value of the first input in the collection. You can use each() in order to iterate over the whole collection:

setTimeout(() => { $(document).ready(function () { $('input').each(function() { if ($(this).val().indexOf('Select') !== -1) { $(this).css('color', '#6D6E71'); } }); }); }, 500); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.