By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

Change background color on clicking button

100+
P: 158
I need to make an input button for a form i have created in html, But what i need to do is when the button is clicked it turns the background to green or whatever and when clicked again it turns back to the color before.

Heres what i have but that bg color stays the same.

Expand|Select|Wrap|Line Numbers
  1.   <input style="background-color:green;" TYPE="submit" name="vars" VALUE="Enter">
Nov 20 '07 #1
Share this Question
Share on Google+
5 Replies


drhowarddrfine
Expert 5K+
P: 7,435
html/css cannot do this. You need to check with javascript.
Nov 21 '07 #2

Expert 100+
P: 390
write one javascript function for the onclick event
insdie that function write the code for button background change
Nov 21 '07 #3

Death Slaught
100+
P: 1,137
If you post this in the JavaScript forum they're going to give you same thing I am now, so i'll save you a post.

[HTML]<html>

<head>
<script type="text/javascript">
//JavaScript can go in either the head or body elements
//You use the script tag to tell the browser you are using script
//The type attribute tells the browser what type of langauge it is

function change() {
document.bgColor="red";
}

//This generates a function, and that functions name is change
/*document.bgColor tells the browser that what you're changing is in the document of the page, and that your changing the background color (bgColor)*/
</script>
</head>

<body>
<form>

<input type="button" onClick="change()" value="Change" />
<!-- onClick tells the browser that when this is clicked you want this to happen, in this case you want to execute the function change-->

</form>
</body>

</html>[/HTML]

If you have any more questions on this please ask them in the JavaScript forum.

Hope it helps, Thanks, Death
Nov 21 '07 #4

P: 4
You cant do that in HTML.
Nov 27 '07 #5

Death Slaught
100+
P: 1,137
You cant do that in HTML.
I believe that drhowarddrfine already said that, please read others post as well as the origonal posters.

Furthermore I already gave him the answer so unless there is a problem there, there's no need to continue this thread.

Thanks, Death
Nov 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.