views:

443

answers:

2

Hi! I'd like to style 'input.submit' of a form (hover effect for IE) using JS and tried the following which doesn't work unfortunately.

<!--[if IE]>
<script type="text/javascript">

// CHANGE SUBMIT STYLE
var foo = document.getElementByClass('input.submit');
foo.onmouseover = this.style.border-color='#000000'; this.style.color='#000000';
foo.onmouseout = this.style.border-color='#888888'; this.style.color='#888888';
foo.onclick = this.style.border-color='#000000'; this.style.color='#000000';

</script>
<![endif]-->

Could you please fix this for me? TIA, Dan

+2  A: 

It should be

foo.onmouseover = function() {this.style.borderColor='#000000'; this.style.color='#000000';}

Max Kramnik
Thanks for your instant reply! I've tried this but there's no effect at all. Could I use 'getElementByID' instead of 'getElementByClass' because I suggest that may cause the problem? It's '#HCB_comment_box input.submit' if that helps...
Dan
`document.getElementById` is preferred to `getElementsByClass` as it uses a lookup table which is much faster, but it would be possible to use the code at http://domscripting.com/blog/display/18 and go `var foo = getElementsByClass(document.body, 'submit', 'input')[0];`. There isn't a DOM `document.getElementByClass` as far as I know though.
David Morrissey
Firebug (http://getfirebug.com/) is excellent for debugging this kind of stuff BTW :-)
David Morrissey
So, if I understand you right the problem is that there's no ID specified for the input and `document.getElementByClass('input.submit');` resp. `document.getElementByClass('submit');` won't work, right?
Dan
you'd use `document.getElementById('submit')` if the tag is `<input type="submit" id="submit">` as it doesn't need the `input.*`, but see also my answer below
David Morrissey
A: 

The answer is considerably more complicated if the elements are generated by an external javascript script. You'll need to find the element first, so the by id and class approaches won't work unless they already have one - see the type solution below.

Find by id:

The following is preferred, you need to add an id to the input submit e.g. <input type="submit" id="submit"> to reference it by:

// CHANGE SUBMIT STYLE
var foo = document.getElementById('submit');
foo.onmouseover = function() {this.style.borderColor='#000000'; this.style.color='#000000';}
foo.onmouseout = function() {this.style.borderColor='#888888'; this.style.color='#888888';}
foo.onclick = function() {this.style.borderColor='#000000'; this.style.color='#000000';}

but the following should also work:

Find by class:

you'll need to specify a class e.g. <input type="submit" class="submit"> in this case. getElementsByClass doesn't look for the type, it looks for the class.

<script type="text/javascript">

function getElementsByClass(node,searchClass,tag) {
  var classElements = new Array();
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("\b"+searchClass+"\b");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
return classElements;
}

// CHANGE SUBMIT STYLE
var foo = getElementsByClass(document.body, 'submit', 'input')[0];
foo.onmouseover = function() {this.style.borderColor='#000000'; this.style.color='#000000';}
foo.onmouseout = function() {this.style.borderColor='#888888'; this.style.color='#888888';}
foo.onclick = function() {this.style.borderColor='#000000'; this.style.color='#000000';}

</script>

Find by type:

You could find by type if you use the following:

function getElementByType(node,type,tag) {
  var els = node.getElementsByTagName(tag);
  for (var x=0, x<els.length; x++) {
    if (els[x].type && els[x].type.toLowerCase() == type) {
      return els[x]
    }
  }
}
var foo = getElementByType(document.body, 'submit', 'input')
...

What I would do is:

<div id="externalElms">
    (<script> here)
</div>

then use var foo = getElementByType(document.getElementById('externalElms'), 'submit', 'input') so it doesn't have to go through every element on the page.

David Morrissey
It's getting much clearer now. I would have added an ID for the input and then used the document.getElementById method but the input itself is created by an external JS script. So, I'll have to give the alternative solution a try. Thanks, David!
Dan
Another idea: Wouldn't it be possible to attach an ID to the input first (using JS) and then call it by document.getElementById?
Dan
@Dan: Added a faster option, but you need to find the element before setting the `id`, so it may not be worth it unless you need to access it again using `document.getElementById`
David Morrissey
Thanks so much, David. I think I'll get this going... Bye.
Dan