[Fixed]-Dynamically assigning ID to an element with jQuery

1👍

this.id is undefind inside $.get. SO you should change your code like this

$(document).ready(function() {
      $('.vote_up').on('click', function() {
        var id = this.id;
        $.get("/vote_up/" + id + "/", function(data) {
          $('#voted' + id).html(data);
        });
      });
      $('.vote_down').on('click', function() {
        var id = this.id;
        $.get("/vote_down/" + id + "/", function(data) {
          $('#voted' + id).html(data);
        });
      });
    });
👤Anoop

0👍

You can use Jquery addClass() method to assign classes dynamically to an element.

Example :

$("#ID").addClass("selected");

Or if you just want to add an style to and element you can use css() method

Example:

$("#ID").css( "color", "red" );

Hope this may help you

Leave a comment