Zvezdas1989
Zvezdas1989

Reputation: 1465

JQuery onClick and HTML

Hi guys so I'm using this simple function to flip card and I'm having some issues. This is the function:

function flipIt() {
    $('.card').toggleClass('flipped');
}

Now when I call it directly in html like this:

<div class="card" onclick="flipIt()">

it works well but when i try to call it directly in my JS file it doesn't work.

I tried this:

$('.card').onclick(flipIt());

and this:

$('.card').on(click, function(){
    $('.card').toggleClass('flipped');
});

Here is mine JSBin so you can see my whole code.

Upvotes: 0

Views: 990

Answers (1)

Arun Kumar Mohan
Arun Kumar Mohan

Reputation: 11915

The event(click) should be in quotes.

$('.card').on('click', function() {
  $('.card').toggleClass('flipped');
});

You can actually refer to the card that was clicked using this.

$('.card').on('click', function() {
  $(this).toggleClass('flipped');
});

Upvotes: 4

Related Questions