0

I have a little problem adding a class to a span element and so coloring it in order to perform simple validation.

Here is my js:

     function validateKey(){
        var length = $('#appkey').val().length; 
        if(length != 8){
            $('#appkey').addClass('error');
            $('#appKeyInfo').addClass('error');
            return false;
        }else{
            $('#appkey').removeClass('error');
            $('#appKeyInfo').removeClass('error');
            return true;
        }
      }

And html:

<label>KEY</label></br>
<input type="text" id="appkey" value=""/></br>
<span id="appKeyInfo">Dein App-Key aus 8 Ziffern</span>  

And the jsfiddle: example

Any ideas?

UPDATE: coloring of appKeyInfo fails, coloring appkey works. When I remove color:red and type font-weight:bold instead the text is bold on error. when I remove color definition of appKeyInfo the text can be colored red on error, strange thing, but I need a font color for the appKeyInfo

9
  • Works for me in FX - what is the issue exactly? Commented Jan 13, 2011 at 7:28
  • Is your CSS class for .error targeting only #appKey, or perhaps only input boxes? Commented Jan 13, 2011 at 7:29
  • i updated my answer, #appKeyInfo keeps grey, strange thing Commented Jan 13, 2011 at 7:29
  • The box goes pink in safari. What's the problem exactly? Commented Jan 13, 2011 at 7:30
  • 1
    Well at least you've ruled out that it wasn't a jQuery error, so you should update the question :) Try adding span#appKeyInfo.error to be explicit and see if that works, then work backwards. Commented Jan 13, 2011 at 7:44

2 Answers 2

2

The declaration for span#appKeyInfo takes precedence since it is an id you are styling. Try using color:red !important to force the override

Edit

Just a note, you can use multiple selectors in your jQuery. Like this:

$('#appkey', '#appKeyInfo').removeClass('error');
Sign up to request clarification or add additional context in comments.

Comments

0

your comparison operator is wrong, use less than operator :

  function validateKey(){
    var length = $('#appkey').val().length;    
    if(length < 8){
        $('#appkey').addClass('error');
        $('#appKeyInfo').addClass('error');
        return false;
    }else{
        $('#appkey').removeClass('error');
        $('#appKeyInfo').removeClass('error');
        return true;
    }
  }

1 Comment

sorry but I exactly 8 chars are required. this question is about the coloring!

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.