Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Style Swap with Javascript

  1. #1
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Style Swap with Javascript

    The following code will change the class name of an element (in this case div) when hovered over. By defining the properties for the new class using CSS you can change the style when hovered over.

    What I would like to be able to do is target any elements, not just div, that have a particular class already defined. Ideally there would be the option for including more than one class. In other words if an element has a class name "this" change the class name on mouseover to "that" and go back to the original on mouseout, or if an element has a class name "what" change the class name on mouseover to "why", otherwise do nothing.

    Writing it inline works <div class="this" onmouseover="className='that'" onmouseout="className='this'"> but I don't wan't to have to add a lot of extra code to my mark up. I honestly am pretty clueless when it comes to javascript and have exhausted my limited knowledge of things to try. I don't even know if I'm on the right track. Anyone feel like taking a stab at it please?

    function styleswap(){
    var el = document.getElementsByTagName("div");
    for (i = [0]; i < el.length; i++) {
    el[i].onmouseover=function() {
    this.className="jshover";
    }
    el[i].onmouseout=function() {
    this.className="";
    }
    }
    }
    window.onload=function(){
    styleswap();
    }

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    /*
        Written by Jonathan Snook, http://www.snook.ca/jonathan
        Add-ons by Robert Nyman, http://www.robertnyman.com
    */
    
    document.getElementsByClassName = function(oElm, strTagName, strClassName){
        var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName).
          arrReturnElements = new Array();
        strClassName = strClassName.replace(/\-/g, "\\-");
        var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
        for(var i=0; i<arrElements.length; i++)
            if(oRegExp.test(arrElements[i].className))
                arrReturnElements.push(arrElements[i]);
        return (arrReturnElements);
    };
    
    function setClassSwitch(o, f) {
      var e = getElementsByClassName(document, "*", o);
      for(var i=0;i<e.length;i++) {
        e[i].origClass = o;
        e[i].hoverClass = f;
        e[i].onmouseover = function() {
          this.className = this.hoverClass;
        };
        e[i].onmouseout = function() {
          this.className = this.origClass;
        };
      }
    }
    You definitely want to call that onload.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey,
    But I keep getting an error Object expected that correspnds to this line:
    var e = getElementsByClassName(document, "*", o);

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Sorry, I decided to put the function in document for some reason. That line should be:
    Code:
    var e = document.getElementsByClassName(document, "*", o);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Now it's telling me strTagName is undefined here:
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName).
    and length is null or not an object here:
    for(var i=0;i<e.length;i++) {

    I also don't understand how to say what elements or class I want this to affect

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    OK, let's revert to the original, I've no idea why I wanted to put this in document in the first place.
    Code:
    /*
        Written by Jonathan Snook, http://www.snook.ca/jonathan
        Add-ons by Robert Nyman, http://www.robertnyman.com
    */
    
    function getElementsByClassName(oElm, strTagName, strClassName) {
        var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName),
          arrReturnElements = new Array();
        strClassName = strClassName.replace(/\-/g, "\\-");
        var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
        for(var i=0; i<arrElements.length; i++)
            if(oRegExp.test(arrElements[i].className))
                arrReturnElements.push(arrElements[i]);
        return (arrReturnElements);
    }
    
    function setClassSwitch(o, f) {
      var e = getElementsByClassName(document, "*", o);
      for(var i=0;i<e.length;i++) {
        e[i].origClass = o;
        e[i].hoverClass = f;
        e[i].onmouseover = function() {
          this.className = this.hoverClass;
        };
        e[i].onmouseout = function() {
          this.className = this.origClass;
        };
      }
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't get it, now I'm getting an error "undefined is null or not an object"

    I followed the link to the site http://www.robertnyman.com/ and found the code on this pageThe Ultimate getElementsByClassName Even if I copy and paste either example of the codes there exactly I still get the same error, so I doubt its the code.

    This is from the site:
    Some ways to call it
    To get all a elements in the document with a “info-links” class.
    getElementsByClassName(document, "a", "info-links");
    To get all div elements within the element named “container”, with a “col” class.
    getElementsByClassName(document.getElementById("container"), "div", "col");
    To get all elements within in the document with a “click-me” class.
    getElementsByClassName(document, "*", "click-me");
    And Twey mentioned
    You definitely want to call that onload.
    I havent got a clue what to do with the ways to call it from the site, anything I try just causes more errors. So can someone please explain to me exactly I need to do make this work in clear simple steps that some one who does not get javascript will understand. Lets say I have a class "this" that I want change to a class "that" and another class "what" that I want to change to "why" how would I write that in?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    window.onload = function() {
      setClassSwitch("this", "that");
      setClassSwitch("what", "why");
    };
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much Twey! That is absolutely perfect, it does exactly what I need.

  10. #10
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I've run into a small problem. If I have a class (or classes) assigned to an element already, it seems they get wiped out when moused over and only the class applied by the javascript remains.

    For example, if I have the onload function and CSS written like this:
    Code:
    window.onload = (function() {
      setClassSwitch("this", "thisHover");
      setClassSwitch etc...
    })
    
    .center {
     text-align: center;
    }
    .this {
     background: #0ff;
    }
    .thisHover {
     background: #fc9;
    }
    Everything is fine in the first div, but the text doesn't stay centered in the second:
    Code:
    <div class="this">Some Text</div>
    <div class="this center">Some Text</div>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •