templating button actions using js

  • Follow


Hi there,

I'm currently using javascript to format buttons as follows.

<input type="submit" value="Submit" title=""    onmouseover="this.style.background='blue'" onmouseout="this.style.background='#FF1199'">

What I would like to know is is there a way that I can have a templated button? This would mean that the if I change the formatting so that the onmouseover colour is 'red' instead in one place it would affect all buttons?

Cheers,
A
0
Reply aaron 3/26/2011 11:29:40 AM

aaron wrote:

> I'm currently using javascript to format buttons as follows.
>
> <input type="submit" value="Submit" title=""
> onmouseover="this.style.background='blue'"
> onmouseout="this.style.background='#FF1199'">
>
> What I would like to know is is there a way that I can have a
> templated button? This would mean that the if I change the formatting
> so that the onmouseover colour is 'red' instead in one place it would
> affect all buttons?

You can do that without using Javascript at all, using just the CSS rules

input[type="submit"] { background: #f19; }
input[type="submit"]:hover { background: blue; }

This fails on some old versions of IE, but on the other hand, on supporting 
browsers, it works even when Javascript is disabled. Note: It also fails on 
modern versions of IE in "Quirks Mode", so if your page currently works in 
"Quirks Mode", this might not be a feasible way. The simplest way to avoid 
"Quirks Mode" is to have
<!DOCTYPE html>
as the first line of the HTML document.

If you want (or need) to do this in Javascript, you can use e.g. the 
following (executed after the document is loaded, so e.g. put it in your 
onload event handler or place the script element at the very end of the 
document body):

var input = document.getElementsByTagName("input");
for(var i = 0; i < input.length; i++) {
  if(input[i].type == "submit") {
     input[i].onmouseover = function () {this.style.background = "blue"};
  input[i].onmouseout = function () {this.style.background = "#f19"}; }}

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/ 

0
Reply Jukka 3/26/2011 1:16:11 PM


1 Replies
236 Views

(page loaded in 0.045 seconds)

Similiar Articles:













7/23/2012 2:59:42 PM


Reply: