Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Really simple question. I have the following code, and would like to consolidate it down as much as possible. Can't get it to work though.. tried using commas etc. Is there a way to call more than one ID in the GetElementByID method? New to Javascript : /

$("#set50").live("click", function() {
    document.getElementById("statusBox50").setAttribute("class", "statusBoxSelected");
    document.getElementById("statusBox25").setAttribute("class", "statusBox");
    document.getElementById("statusBox75").setAttribute("class", "statusBox");
    document.getElementById("statusBox100").setAttribute("class", "statusBox");
    $(".statusbar").animate({
        width: '115px'
    }, 500);    
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
877 views
Welcome To Ask or Share your Answers For Others

1 Answer

No, but since you're using jQuery anyway, you can use that. This will add the statusBox class to the elements with the IDs statusBox25, statusBox75, and statusBox100:

$("#statusBox25, #statusBox75, #statusBox100").addClass('statusBox');

Alternatively, if you want to remove all of the existing classes and replace them all with statusBox like your original code as doing, you could use this:

$("#statusBox25, #statusBox75, #statusBox100").attr('class', 'statusBox');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...