Javascript change textbox background


 

Javascript change textbox background

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

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.

Ads