0

I think i have a simple issue. I have a set of fields on my form:

<input class="one-third" id="Firstname" type="text"/>

When one of these fields are blank, my validation should highlight the field (red boarder).

I have attempted the fiddle - http://jsfiddle.net/oampz/Ybxk2/1/

$(".submitForm").click(function() {

    if($("#Firstname").val() == ''){
        alert('Input can not be left blank');

        $("#Firstname").addClass("highlight");
    }   

    //document.forms[0].submit();

});

But it doesnt seem to work.

Should i be removing my original class?

Thanks

1
  • 1
    I don't know why this question was downvoted, he tried, he shared his code, as well as the demo, he wasn't aware of specificity, anyways upvoted, wasn't a candidate to downvote.. Commented Jan 29, 2014 at 13:37

3 Answers 3

4

NO, you don't have to remove original classes.. specificity is the issue there, so you will need a more specific selector.

input[type="text"] Overrides .highlight


input[type=text].highlight {
    font-size:150%;
    color:red;
    border: 1px solid red;
}

Demo


Don't know what's specificity? Learn Here..

If you want to calculate specificity, here's an handy tool, if you feed your selectors in there, the score for input[type="text"] will be 11 whereas for .highlight is 10

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

3 Comments

@OamPsy Don't worry, it's an absolute perfect question and you welcome :)
@user3004356 You shouldn't answer here, comment on your question, I provided you the link because you have a similar issue
@user3004356 Comment over here, not here, this is not your question, I just told you to refer this because you are not able to understand specificity, so don't comment here, comment on the answer I posted on your question
1

!important tells the browser to overwrite any CSS rules that affect the property.

.highlight {
    font-size:150%;
    color:red!important;
    border: 1px solid red!important;
}

Demo

Comments

1

I will suggest you to keep the changing styles in two different classes instead of overriding as shown by Mr.Alien.

something like .green and .red.

.green{
    border: 1px solid #ababab;
}

.red{
    border: 1px solid red;
}

This will help you not to get confuse.

Working Fiddle

In short, don't keep the changing styes as common style to all elements. Not a good design.

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.