Changing javascript so CSS class is used

Discussion in 'JavaScript' started by mark_s, Jan 8, 2008.

  1. #1
    Hi, I'm not that great at javascript so can someone work out how I add "class=xxx" into the link that is produced from this script?

    Javascript,

    var COLLAPSABLE_PARENT_NAME = "more";
    var COLLAPSABLE_PARENT_TYPE = "span";
    var COLLAPSABLE_CHILD_TYPE = "p";
    
    var COLLAPSABLE_EXPAND = "(show)";
    var COLLAPSABLE_SHRINK = "(hide)";
    
    init = function() {
        if(document.getElementById && document.createTextNode) {
            var entries = document.getElementsByTagName(COLLAPSABLE_PARENT_TYPE);
            for(i=0;i<entries.length;i++)
                if (entries[i].className==COLLAPSABLE_PARENT_NAME)
                    assignCollapse(entries[i]);
        }
    }
    
    assignCollapse = function (div) {
        var button = document.createElement('a');
        button.style.cursor='pointer';
    	button.style.color='#666666';
    	button.style.fontWeight='bold';
    	    button.setAttribute('expand', COLLAPSABLE_EXPAND);
        button.setAttribute('shrink', COLLAPSABLE_SHRINK);
        button.setAttribute('state', -1);
        button.innerHTML='dsds';
        div.insertBefore(button, div.getElementsByTagName(COLLAPSABLE_CHILD_TYPE)[0]);
    
        button.onclick=function(){
            var state = -(1*this.getAttribute('state'));
            this.setAttribute('state', state);
            this.parentNode.getElementsByTagName(COLLAPSABLE_CHILD_TYPE)[0].style.display=state==1?'none':'block';
            this.innerHTML = this.getAttribute(state==1?'expand':'shrink');
        };                   
        button.onclick();
    }
    
    
    window.onload=init;
    Code (markup):
    HTML produced from it,

     <a state="1" shrink="(hide)" expand="(show)" style="cursor: pointer; color: rgb(102, 102, 102); font-weight: bold;">(show)</a>
    Code (markup):
     
    mark_s, Jan 8, 2008 IP
  2. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I think adding another setAttribute would do it:

    element.setAttribute("class, "xxx");
    Code (markup):
    Could you show a live example of your code in action?
     
    KatieK, Jan 8, 2008 IP
  3. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply.

    Your code didn't work unfortunately, Dreamweaver suggested it broke the rest of the javascript.

    I have used this script for my news articles and results page:
     
    mark_s, Jan 8, 2008 IP
  4. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #4
    D'oh! I just noticed that I forgot the closing " on the end of class. Try this:

    element.setAttribute("class", "xxx");
    Code (markup):
    I'll have a look at your pages.
     
    KatieK, Jan 8, 2008 IP
  5. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That also didn't work... the expand link just didn't show up.


    assignCollapse = function (div) {
        var button = document.createElement('a');
        button.style.cursor='pointer';
    	button.style.color='#666666';
    	button.style.fontWeight='bold';
        element.setAttribute("class", "xxx");
        button.setAttribute('expand', COLLAPSABLE_EXPAND);
        button.setAttribute('shrink', COLLAPSABLE_SHRINK);
        button.setAttribute('state', -1);
        button.innerHTML='dsds';
        div.insertBefore(button, div.getElementsByTagName(COLLAPSABLE_CHILD_TYPE)[0]);
    Code (markup):
     
    mark_s, Jan 8, 2008 IP
  6. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Ah ha! Use this:

    	button.setAttribute("class", "xxx");
    Code (markup):
    It needed to be button, not element.

    You can take a look at the attached file that I used to get it working.
     

    Attached Files:

    KatieK, Jan 8, 2008 IP
  7. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Unfortunately IE doesn't accept the class when it is set that way.

    Do it like this:

    button.className = "xxx";
     
    MMJ, Jan 9, 2008 IP
  8. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks for all your help people.

    I used MMJ's method.
     
    mark_s, Jan 9, 2008 IP