Search code examples
javascriptdomsimple-html-dom

change style background property by using getElementById


How can I change my textbox color if I hit the error function? I try the following code but it's not working only the error message will display.

<Script>
function fnName(ID)
{
    var flag = true;
    if(document.mainform.NAME.value == "")
    {
        error_function("Please enter the Name.",ID);
        flag = false;
    }
    else
    {
        release_function(ID)
        flag = true;
    }
    return flag;
}
function error_function(message,IDs)
{
        var ID = '"' + IDs + '"';

        document.getElementById("proposererrMsg").innerHTML     = message;
        document.getElementById("proposererrMsg").style.display = "inline";
        document.getElementById(ID).style.background  = "#DDDDDD";  //this line not working
         document.getElementById(ID).style.border    = "thin solid red"; // this line not working

}
</script>

 <label id="proposererrMsg" class="errMsg"></label> 
 <input type="text" name="NAME" id="NAME" value="<%=NAME%>" onblur="fnName(this.id);" />

Solution

  • The ID you use has unnecessary quotes, so you try to reference an element with a nonexistent ID, just use IDs parameter directly:

    document.getElementById(IDs).style.background = "#DDDDDD";