tags:

views:

35

answers:

3

I have a function which wanders through the elements in a page and adjusts the font size

function changebuttonsize (sz)
{
  var z = document.body.getElementsByTagName("*");  
  for (var i=0; i<z.length; i++)
  {
    if (z[i].id=="BTSz")
    {
      z[i].style.fontSize = sz;
    }
  }
}

I use it to change the size of the following buttons

<input id="BTSz" type="button" style="font-size:16px;" value="Back"  onclick="history.go(-1);"/>
<input id="BTSz" type="button" style="font-size:16px;" value="Index" onclick="location.href='Index.html';"/>
<input id="BTSz" type="button" style="font-size:16px;" value="Home"  onclick="location.href='Introduction.html';"/>

The problem is that I had to include a style clause in the button definition or the change did not take effect. Is there some way to correct this? Do I somehow have to create a style area for the element?

A: 

How about this?

function changebuttonsize (sz)
{
  var z = document.body.getElementsByTagName("*");  
  for (var i=0; i<z.length; i++)
  {
    if (z[i].id=="BTSz")
    {
      z[i].style = 'font-size:'+sz+'px';
    }
  }
}
Jayesh
A: 

Have you ever considered using jQuery? Using jQuery you are able to select specific items (using selectors) in the DOM and add a class to anything.

JoshVarga
A: 

Using the same value for the "id" attribute of several elements is a recipe for all sorts of weirdness. I'm not sure it'd cause whatever problems you're having, but while fooling around looking for a solution it'd be a good idea to fix that.

Usually, the "class" attribute is used to store this sort of "trait" values. You can also use a "data-" attribute if you're coding for HTML5:

<input type='button' data-MikeD='BtNSz' ... >

and then use getAttribute to fetch the value:

  var value = theButton.getAttribute("data-MikeD");
Pointy
@pointy: Things progressing well. Just discovered Mozilla and Opera have built in getElementsByClassName but IE does not. Found many replacements on web but not with same arguments. I'm tempted to ignore getElementsByClassName and page through all the tags but is there a better way?
Mike D
Well, I generally don't like this as a primary response, but honestly a librarly like jQuery makes this kind of thing a *lot* easier. Things like this become almost completely painless.
Pointy