简体   繁体   中英

how to change textbox background color through javascript?

my javascript-

function validate_loginform(loginform) 
{
var uid = loginform.uid.value;
var pass = loginform.pass.value;
if(uid == "") 
  {

    color('uid');       
    return false;
  }
if(pass == 0) 
  {
    color('pass');
    return false;
  }

return true;

}

function color(traget)
{
var targetbox = document.getElementById(target);
targetbox.style.backgroundColor="red";
}

but background color is not getting changed even it is not returning fasle value. if I remove the color('uid'); nad put alert("user name required"); then this script is working fine.Whats wrong?
it backgroundColor in actual program I just missed it here only

使用jQuery,您可以尝试以下操作:

 $("#textbox").css("background-color", "red");

dont call color function, change color inside if condition like-

if(uid == "") 
  {     
    //alert("You must enter User ID.","error");
    loginform.uid.style.borderColor='red';
    loginform.uid.focus();
    return false;
  }

Typo?

backgroungColor
         ^

Update

Typo?

function color(traget)
               ^^^^^^
{
var targetbox = document.getElementById(target);

Seriously, actual code does matter.

Beware your spelling. It should be "target", not "traget".

function color(traget)

您在函数标头中将目标拼写错误,在函数的最后一行中将背景拼写错误。

只需从color('uid')中删除单引号(')

and write it as color(uid);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM