JavaScript removeClass if addClass clicked? addclass removeclass onclick,addclass removeclass click

The 'click' event is assigned to your 'image-clicker' elements, but the 'clicked' class is added to the hidden field. The element references are mixed up, and the code needs to check to see if the image has already been clicked.

This might be closer to what you're trying to do:

<script>
  $('.image-clicker').click(function() {
    $('#ranking').val($(this).data('ranking'));
    var already_clicked = $(this).hasClass('clicked');
    $('.clicked').removeClass('clicked')
    if (!already_clicked) {
      $(this).addClass('clicked');
    }
  });
</script>

The already_clicked check determines if the image is already clicked, so that it can tell whether to keep click it after clearing the existing 'clicked' classes from the other images.

Note that this will allow all images to become 'unclicked'. If you want this to behave like radio buttons, you can leave out the check for already_clicked and always add the 'clicked' class to the element that was clicked. This will do that:

<script>
  $('.image-clicker').click(function() {
    $('#ranking').val($(this).data('ranking'));
    $('.clicked').removeClass('clicked')
    $(this).addClass('clicked');
  });
</script>

In this case, you'll likely also want to start your radio buttons in a known state. In the original markup (ERB), you would want to add the 'clicked' class to one of the image_tag elements. You simply have to add the 'clicked' class to one of the images at creation time, like so:

<%= f.hidden_field(:ranking, id: 'ranking') %>
<%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker clicked']) %>
<%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker') %>
<%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker') %>
<%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker') %>

You can also do this dynamically, based on the value of a variable or field from your model. Assuming that a variable named ranking contains this value, consider this initialization code:

<%= f.hidden_field(:ranking, id: 'ranking') %>
<%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker' + (ranking == '4' ? ' clicked' : '')) %>
<%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker' + (ranking == '3' ? ' clicked' : '')) %>
<%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker' + (ranking == '2' ? ' clicked' : '')) %>
<%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker' + (ranking == '1' ? ' clicked' : '')) %>
Answer:1

Place your code inside document.ready

<script>
    $(document).ready(function(){


         //Place your javascript /jquery code here

    })
</script>
Answer:2

class helloworld { constructor(public message: string){} } var hello = new helloworld('hello vishal'); Console.log(hello.message); I am getting this error at Console.log statement you can see the ...

class helloworld { constructor(public message: string){} } var hello = new helloworld('hello vishal'); Console.log(hello.message); I am getting this error at Console.log statement you can see the ...

My script doesn't works when I use requirejs library to launch it. It's a simple game. After I click "start" program draws squares and when a green square appear the user has to click it as quick as ...

My script doesn't works when I use requirejs library to launch it. It's a simple game. After I click "start" program draws squares and when a green square appear the user has to click it as quick as ...

  1. javascript code works with alert
  2. javascript code only works in debug
  3. how javascript code works
  4. javascript code only works with alert
  5. javascript code doesn't work
  6. javascript code doesn't work in chrome
  7. javascript code won't work
  8. javascript code dont work
  9. how javascript code coverage works

I want to check if 1) a number is even or odd 2) print a message saying "not a number" if the value is not a number. I did with the if else statements but I'm trying to do it with the ternary one for ...

I want to check if 1) a number is even or odd 2) print a message saying "not a number" if the value is not a number. I did with the if else statements but I'm trying to do it with the ternary one for ...

I have a link button that goes to another page, it works, but i want the link to go to a spicific part of that page. i know i should use jquery for thisand for some reason its not jumping to the ...

I have a link button that goes to another page, it works, but i want the link to go to a spicific part of that page. i know i should use jquery for thisand for some reason its not jumping to the ...

  1. button link to part of page