Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing textbox border colour using javascript

I'm doing form validation. when the form input is incorrect i add a red border to the textbox:

document.getElementById("fName").style.borderColor="#FF0000"

this then gives me a 2px red border. what i want to do is if the user putas in a correct value to go back to the original border...

can someone tell me how i change the border size and colour in javascript

like image 449
kevin Avatar asked Apr 25 '11 20:04

kevin


4 Answers

Use CSS styles with CSS Classes instead

CSS

.error {
  border:2px solid red;
}

Now in Javascript

document.getElementById("fName").className = document.getElementById("fName").className + " error";  // this adds the error class

document.getElementById("fName").className = document.getElementById("fName").className.replace(" error", ""); // this removes the error class

The main reason I mention this is suppose you want to change the color of the errored element's border. If you choose your way you will may need to modify many places in code. If you choose my way you can simply edit the style sheet.

like image 99
John Hartsock Avatar answered Sep 27 '22 19:09

John Hartsock


document.getElementById("fName").style.border="1px solid black";
like image 44
josh.trow Avatar answered Sep 27 '22 18:09

josh.trow


You may try

document.getElementById('name').style.borderColor='#e52213';
document.getElementById('name').style.border='solid';
like image 23
CEN Avatar answered Sep 27 '22 19:09

CEN


Add an onchange event to your input element:

<input type="text" id="fName" value="" onchange="fName_Changed(this)" />

Javascript:

function fName_Changed(fName)
{
    fName.style.borderColor = (fName.value != 'correct text') ? "#FF0000"; : fName.style.borderColor="";
}
like image 23
BumbleB2na Avatar answered Sep 27 '22 20:09

BumbleB2na