changing textbox border colour using javascript

后端 未结 7 616
心在旅途
心在旅途 2020-12-30 02:20

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

document.getElementById(\"fName\").style.borderColor=\"#FF000         


        
7条回答
  •  梦谈多话
    2020-12-30 02:47

    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.

提交回复
热议问题