1

jQuery:

$(document).ready(function() {
    $('.text').load(function() {
        if (this.value == this.title) {
            $('.text').addClass("highlight");
        }
    });
});

CSS:

.highlight
{
  background: #FFA9B8;
  border: 1px solid red;
}

HTML

<label for="first_name">first name: </label>
<input type = "text" name = "first_name" class = "text" value = "First..." title = "First..." />
<label for="last_name">last name: </label>
<input type = "text" name = "last_name" class = "text" value = "Last..." title = "Last..." />

I essentially want to add the class "highlight" to the background of the 2 input fields when the page is loaded...only when both the title and value are the same.

Thank you -Art

0

1 Answer 1

2
$(document).ready(function(){
   $('.text').filter(function(){
        return this.value === this.title
   }).addClass('highlight')
});
Sign up to request clarification or add additional context in comments.

4 Comments

I think you need a semi-colon after the end of the return statement and the end of the addClass() but otherwise should work.
@MarkSchultheiss semi-colon is optional in JS.
Sometimes, my comments should have indicated "to pass jslint" add semi-colons in the noted places.
@MarkSchultheiss Yes, most of the times. jslint is another story.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.