2

I checked out some other posts on here but still couldn't get this issue to work. I have several elements in my html with the class cardContainer:

<div class="cardContainer">
<div id="card2" class="block" onclick="changeClass()">
</div>
</div>


<div class="cardContainer">
<div id="card3" class="block" onclick="changeClass()">
</div>
</div>

For each onClick event I would like to call this JS function:

<script type="text/javascript"> 
function changeClass(){
if(document.getElementById("card2").className == "block")
document.getElementById("card2").className += " rotated";

else
document.getElementById("card2").className = "block";
}
</script> 

What I would like to do is include the card3 id, so it fires the same function on click. How would I be able to combine the ids "card2" and "card3" in the javascript so the function works?

I get that if I use getElementById, I can only get one and not multiple ids/classes, but I tried using getElementsByClassName for example without success. Also looked at other posts but couldn't get this to work based on the suggestions... I am new to Javascript and not quite sure on how to approach this.

Thanks for your help!

3
  • What are you trying to do? Commented Jun 19, 2014 at 15:33
  • It's not clear what you are trying to do here. First of all, you are getting element by id card2 and you have no card2 in your markup. Make sure to include all your code and explain what you are trying to accomplish. Commented Jun 19, 2014 at 15:34
  • getElementById is meant to be used on ids, which are unique. With getElementsByClassName you will get no ids but classes. Commented Jun 19, 2014 at 15:41

4 Answers 4

4

Try this:

HTML

<div class="cardContainer">
    <div class="card block">Click Here</div>
    <div class="card block">Click Here</div>
    <div class="card block">Click Here</div>
</div>

JavaScript

var cards = document.querySelectorAll(".card");
for (var i = 0; i < cards.length; i++) {
    var card = cards[i];
    card.onclick = function () {
        if (this.classList.contains("block")) {
            this.classList.add("rotated");
            this.classList.remove("block");
        }
        else {
            this.classList.add("block");
            this.classList.remove("rotated");
        }
    };
}

Here is the Demo

Compatibility table for support of querySelector/querySelectorAll: Can I Use

Sign up to request clarification or add additional context in comments.

Comments

2

You can pass the id of the div being clicked on your changeClass function:

<div id="card3" class="block" onclick="changeClass(this.id)">

This way, it will be easier to handle your class switching process:

function changeClass(id) {
    var div = document.getElementById(id);
    switch (id) {
        case "card2": {
            if (div.className == "className") {
                div.className = "anotherClassName";
            }

            break;
        }
        case "card3": {
            if (div.className == "block") {
                div.className = "rotated";
            }

            break;
        }

        default: {
            // any other case
        }
    }
}

3 Comments

Thanks, but where would I accommodate the else statement. What's the structure of adding other cases and the else statement? Thanks Eder!
Please find above the updated coded, showing how to add new cases. If you need a long description of the switch statement on javascript, Please take a look at the documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Thanks Eder, this is perfect! I used it like this: function changeClass(id) { var div = document.getElementById(id); switch (id) { case "card2": { if(document.getElementById("card2").className == "block") document.getElementById("card2").className += " rotated"; else { document.getElementById("card2").className = "block"; } break; } case "card3": { if(document.getElementById("card3").className == "block") document.getElementById("card3").className += " rotated"; else { document.getElementById("card3").className = "block"; } break; } } } Thank you!
0

use document.getElementsByClassName( doesn't work on ie<9 or FF<3) if you don't care about older browsers and if you do then i suggest you to use jquery, or just sizzle.js to use css selectors

Comments

0

I think you're looking for something like this?. (assuming you're ok with using jQuery)

http://jsfiddle.net/LqpKt/

<div class="cardContainer">
  <div id="card1" class="block"></div>
</div>
<div class="cardContainer">
  <div id="card2" class="block"></div>
</div>
<div class="cardContainer">
  <div id="card3" class="block"></div>
</div>

<div id="output"></div>

$('.cardContainer').click(function(e){
    var name = $(this).find('.block').attr('id');
    $('#output').append($('<div>').html('clicked ' + name));
})

1 Comment

Thanks Trent, but I am not sure what the output id would be for? ALl I want to do is have the function run on click of each element in plain javascript. Please advise. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.