2

I did this script. But now i need to convert these id to classes because should I put it in other elements of the page.

card ---> .card

flip ---> .flip

 var init = function() {
      var card = document.getElementById('card');
      
      document.getElementById('flip').addEventListener( 'click', function(){
        card.toggleClassName('flipped');
      }, false);
    };
    
    window.addEventListener('DOMContentLoaded', init, false);

Can you help me?

SOLUTION :

$(function() {
  var card = $('.card');
  
  $('.flip').on( "click", function(){
    card.toggleClass('flipped');
    return false;
  });
});
2
  • Have you defined the toggleClassName function anywhere? It's not standard. Also, it would be better to actually amend the HTML source directly to achieve this. Commented Dec 11, 2015 at 14:20
  • @RoryMcCrossan #card.flipped { -webkit-transform: rotateY(180deg); } Commented Dec 11, 2015 at 14:39

1 Answer 1

2

Since you have tagged with , I would give this:

// Once document is loaded,
$(function () {
  // Change all the `#card`, `#flip` to classes:
  $("#card, #flip").addClass(function () { return this.id; }).removeAttr("id");
});

First it selects all the #card and #flip, there should be only 2 elements. It adds their respective id as class and removes the id attribute.

Snippet

// Once document is loaded,
$(function () {
  // Change all the `#card`, `#flip` to classes:
  $("#card, #flip").addClass(function () { return this.id; }).removeAttr("id");
});
.card {background: #f99;}
.flip {background: #9f9;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="card">Card</div>
<div id="flip">Flip</div>

If you just wanna add the extra classes, then you can use this:

$(function() {
  var card = $('.card');

  $('.flip').on( "click", function(){
    card.toggleClass('flipped');
    return false;
  });
});
Sign up to request clarification or add additional context in comments.

7 Comments

Dont work. This is effect of my code. gyazo.com/dcd3eac86eb840c6a12ef77683fd9621
@Master-Antonio Show your code! Not some image. Did you see my code in the answer?
@Master-Antonio Yes, if you wanna add and remove class, you can use jQuery's toggleClass, but your question was converting the ID to class right?
@Master-Antonio What does this mean? #card ---> .card, #flip ---> .flip?
That id "card" and "flip" becomes classes. Sorry for my english.
|

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.