javascript - Assigning classes or ids to button created dynamically using jquery -


i creating several buttons dynamically. trying assign each on different class or id when clicked on have different output. how add value @ end of each class or id name give each button unique class or id name? here code

socket.on('usernames', function(data){     var $contentwrap = $("#contentwrap").empty();     for(i=0; <data.length; i++){         $input = $('<input type="button" style="width:200px" class= "button"/></br>');         $input.val(data[i]);         $input.appendto($("#contentwrap"));     } });  

can created on click event button created. far created on click even, works first button. here code

$(document.body).on('click','.button', function(e) {      console.log($('.button').val()); }); 

use $(this).val() instead of $('.button').val(). this refer clicked button.

$(document.body).on('click','.button', function(e) {      console.log($(this).val()); }); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -