2

I have an <input> text field in a form. If I click in the text input field and leave it blank when it loses focus then background-color will be changed for the <input>.

I want to use only CSS, not JS or jQuery. Please help me.

If it is not possible only by using CSS then how can I implement it using JS or jQuery?

7
  • Yo do styling with css but manipulating with jQuery Commented Nov 9, 2015 at 2:45
  • Please give me more detail of you requirement. it's not clear about "any change will be occured in the input field like background color will be change for the input field" Commented Nov 9, 2015 at 2:46
  • You have to use both CSS and JavaScript for this. Maybe the jQuery validation plugin would be an option? Commented Nov 9, 2015 at 2:47
  • you can use jQuery Validation Plugin Commented Nov 9, 2015 at 2:50
  • If you're tracking state you need JavaScript. Commented Nov 9, 2015 at 2:54

4 Answers 4

2

Yes,you can use only CSS, not JS or jQuery to achieve the results,for example:

HTML:

<input placeholder="name" type="text" />
<input placeholder=" " type="text" />

CSS:

::-webkit-input-placeholder {
    color: #fff;
    background: red;
}
:-moz-placeholder { /* Firefox 18- */
    color: #fff;
    background: red;
}
::-moz-placeholder {  /* Firefox 19+ */
    color: #fff;
    background: red;
}
:-ms-input-placeholder {  
    color: #fff;
    background: red; 
}

The precondition is that you need a placeholder attribute and it's not none. See this exmaple from this JSFiddle.

You can read this passage Style Placeholder Text about this css property to know more.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do that using JS / jQuery like:

HTML

<input id="name" placeholder="name" type="text" />

jQuery

$('#name').on('blur', function(){
        if(!$(this).val().trim()){
            $(this).css({
                'background-color': 'red'
            })
        }else{
            $(this).css({
                'background-color': 'white'
            })
        }
    })

Please have a look in the following link: JSFIDDLE

Comments

1

The closest pure-CSS approach requires the browser to support the :required pseudo-class. Your input would have the required attribute like so:

<input id="test1" name="test1" type="text" required />

And your CSS would look like:

input:required {
    background-color: red;
}

Here is an example jsfiddle:

http://jsfiddle.net/5L5hx4bo/

However, this would set the style for the input on page load, not when the field lost focus, but this makes more sense, from a CSS/document perspective. For a field to change style based on state, it would be one of three 'states':

invalid :

Which could be handled similar to above but using

input:invalid {
    background-color: red;
}

But a blank text input would not be considered "invalid" and thus would never get styled by such a rule. However, an input with a type like "email" that had a value set to "bogus#value" would get styled, since the value is not considered valid for that type.

required:

The blank text input would be considered a match if required attribute is set, however this would be a match when the form initially loads as well.

focus (negative rule) :

You could have a CSS rule for when the input does not have the state of being in focus, but this is not the same as "on blur". Similar to required, the rule would be applied when the element loads, not exclusively when focus is lost.

Since your requirements for the style are :

  • When an input's state changes from in-focus to not-in-focus
  • When an input is type text and value is empty
  • When an input was most recent element with state of in-focus changing to not-in-focus (so style rule is removed from an input once another input loses focus and has style rule applied)

This would not be something that would be considered a "pure CSS" type state, and thus should be handled via javascript.

1 Comment

Wow, why didn't I think of that? The :required makes it so simple... I need to use CSS3 more.
0

This would not be possible solely with CSS. JS / jQuery are required to do DOM manipulation and scripting in general.

For example, if you had this:

<input type="text" id="input" />

You can use jQuery to check for change and change background color onblur.

var initalValue;
$("#input").focusin(function() {
    initialValue = $(this).val();
});
$("#input").focusout(function() {
    if($(this).val() != initialValue) {
        $(this).css("background-color", "red");
    } else {
        $(this).css("background-color", "white");
    }
});

This will get its initial value and compare it with its value after losing focus. If it is different, the background color of the input will turn red.

See this JSFiddle.

EDIT Upon reading your question again I noticed I read it wrong. My answer made the background red if there was any change, but you want background red if the <input> is blank. You can do this with:

$("#input").focusout(function() {
    $(this).css("background-color", ($(this).val() == "") ? "red" : "white");
});

See the updated JSFiddle.

2 Comments

@Kaiido ... what? What did I do?
@Kaiido Oh I understand now =). But yes, jQuery for DOM manipulation (skipping the horrible document.getElement[s]?by(ClassName|Id|TagName)) is pretty great; however, it is totally do-able by JS.

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.