1

I have a textbox which has a red border as default.

when on focus, (:focus) the border turns green.

When there is 1 or more characters in the textbox, the border turns blue (using this js)

function CheckText()

{

    var elem = document.getElementById('sendto');

    if(elem)

      {      
      if(elem.value.length != 0)
      elem.style.borderColor = '#3366CC';
      }

}

the problem I have is, if I delete all the characters from the box after it has turned blue, it stays blue and does not return to red. the :focus green does not work anymore either after typing in the cell.

Can anyone help?

this is the HTML code for my box:

<input id="sendto" name="sendto" type="text" class="sendsubempty" onBlur="CheckText()" value="<? 
  if(!$sendto){ 
  echo"";
  }else{
  echo"$sendto";
  }
  ?>" maxlength="50" />

for info: class- sendsubempty has a red border.

I have seperate classes for blue & green borders if its easier to use in the javascript.

Please help :(

3 Answers 3

1

Here is a more elegant way to achieve this effect:

http://jsfiddle.net/elias94xx/eqKqS/

HTML

<textarea id="test"></textarea>

CSS

#test { border-color: red; }
#test:focus { border-color: green !important; outline: none; }

JS

document.getElementById("test").addEventListener("keydown", controlBorderColor, false);
document.getElementById("test").addEventListener("keyup", controlBorderColor, false);

function controlBorderColor() {
  if (this.value.length == 0) { this.style.borderColor = "red"; }
  else { this.style.borderColor = "blue"; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

1 more question, how can I apply that to 3 input boxes on the same page?
Using jQuery would be the easiest way: $(".some_input_boxes").on("keydown keyup", controlBorderColor);, otherwise just register more event listensers.
ahhh, ok, Im still learning all of this, but its getting much easier. I understand now. so just register more event listeners and when the relevant box is in focus/active, it will simply call the function for that box as Ive registered the listener....
1

I agree with Loris. But it's not completely correct.

function CheckText()

{

    var elem = document.getElementById('sendto');

    if(elem)
    {      
      if(elem.value.length === 0)
      {
        elem.style.borderColor="";
      }
      else{
        elem.style.borderColor="#3366CC";
      }
    }

}

Here is the jsbin : http://jsbin.com/welcome/70238/edit

1 Comment

Brilliant. much easier. I have 3 boxes (username, password & message) I have added 3 versions of the code and simply changed the function name. Is there an easier way to pass the ID as a parameter to the one function? rather than 3 instances of the function code?
0

The function CheckText should be changed from:

  {      
      if(elem.value.length == 0)
      elem.style.borderColor = '#3366CC';
  }

to:

  {
    if(elem.value.length){ elem.style.borderColor=""; }
    else{ elem.style.borderColor="#36c"; }
  }

1 Comment

This is because inline style has higher specificity than external rule

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.