最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to change textbox background color through javascript? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Aug 5, 2010 at 12:32 nectar asked Aug 5, 2010 at 12:26 nectarnectar 9,67936 gold badges80 silver badges101 bronze badges 4
  • backgroungColor should be backgroundColor, but maybe just a typo? – Felix Kling Commented Aug 5, 2010 at 12:27
  • So many typos, check function color(traget) for starters! – Tom Gullen Commented Aug 5, 2010 at 12:29
  • it backgroundColor in actual program I just missed it here only – nectar Commented Aug 5, 2010 at 12:32
  • What about in function color(traget)? Is that just a typo here as well? – Danny Nimmo Commented Aug 5, 2010 at 12:33
Add a comment  | 

6 Answers 6

Reset to default 6

With jQuery you could try this:

 $("#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)

You've spelt target wrong in your function header and background wrong in the last line of the function.

just remove the single quote (') from color('uid')

and write it as color(uid);
发布评论

评论列表(0)

  1. 暂无评论