jqueryhtmlgetattributedom-node

How to pass 2 parameters by using .on()?


I'm trying to pass 2 parameters to ajax but i'm not sure how to do it. It works passing the id but not the anotherId. I already look into .on() documentation but does not really helped me.

HTML

<button class="btn btn-sm btn-success edit_modal" id="123" anotherId="111">Click</button>

JS

$(document).on('click', '.edit_modal', function() {
            var id1 = this.id;
            var id2 = this.anotherId;
            console.log(id1); //this returns "123" in console
            console.log(id2); //this returns undefined...why?

            $.ajax({
                    ...
                    data: { "id1":id1, "id2":id2 }
                    ...
            });    
});

Solution

  • Change these two:

    var id1 = this.id;
    var id2 = this.anotherId;
    

    to:

    var id1 = $(this).attr("id");
    var id2 = $(this).attr("anotherId");