Share on Google+Share on Google+

Javascript change textbox background

Advertisement
In this section, you will learn how to change the background of textbox.

Javascript change textbox background

In this section, you will learn how to change the background of textbox. Here we have created a textbox and allowed the user to enter the valid name. If the entered name is 'roseindia' then the background changed to green otherwise it will remains red.

Here is the code:

<html>
<
head>
<
title>Change background Color</title>
<
script type="text/javascript">
function check(){
if (document.getElementById('in').value=="roseindia"){
document.getElementById('in').getAttributeNode('class').value="correct";
}else {
document.getElementById('in').getAttributeNode('class').value="incorrect";
}
}
</script>
<
style type="text/css">
.correct {
background: #00ff00;
}
.incorrect
{
background: #f00f00;
}
</style>
</
head>
<
body>
<
p>Enter Valid Name<input id="in" type="text" onkeyup="check();"
class="incorrect" /></p>
</
body>
</
html>

Output:

As the user enters roseindia, backgroud color changed to red.

Advertisements

Advertisement

Posted on: August 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.