0

I'm using the follow jQuery code to style some inputs based on whether they are focused or if they have content in them.

$('.Textbox')
.focus(function() { $(this).addClass("selected") })
.blur(function() { if ($(this)[0].value == '') { $(this).removeClass("selected") } });

My problem is that some of my inputs are automatically filled in by the browser if the user is a returning visitor. Like username and password fields for example. Since the inputs have a value entered in them, I want to style them differently. Unfortunately the jQuery won't apply the styles until the user interacts with the input directly.

Is there any way that I can look to see if the input has a value (without the user interacting with the input) and then give it a class name?

Any help would be greatly appreciated.

1
  • have you tried the change event Commented Aug 17, 2011 at 16:41

2 Answers 2

2

you can do it on "page load"

$(document).ready( function () {
    $('.Textbox').each( function () {
        $this = $(this);
        if ( this.value != '' ) $this.addClass('yourClass');        
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. Did exactly what I needed it to, thanks!
0

the focus() and blur() events are specific to user interaction. You should try the change() event in jQuery, as that gets fired when the value of an input changes, even if that change was triggered by a program and not just the user: http://api.jquery.com/change/

1 Comment

Thanks for the reply. I can see the applications, but unfortunately change() didn't help me on page load. Perhaps the browser auto-fills in the input fields too quickly for this event to trigger.

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.