javascript - how to set button id dynamically and bind click event on it? -
using code generate buttons dynamically , assigning id on it:
function successcbofmissednamazfromdb(tx, results) { if (results != null && results.rows.length > 0 && results.rows != null) { var htmlstring = ""; var temp = 0; (var = 0; < results.rows.length; i++) { htmlstring += '<div class="ui-grid-b">'; htmlstring += '<div class="ui-block-a ui-bar-d" style="font-size:x-medium;height:80px;">' + prayername[i] + '</div>'; htmlstring += '<div class="ui-block-b ui-bar-e" style="font-size:x-medium;height:80px;">' + results.rows.item(i).missing_prayers + '</div>'; alert(""+performednamaz[i]); htmlstring += '<div class="ui-block-c ui-bar-e" style="font-size:x-medium;height:80px;"><input type="number" data-inline="true" data-mini="true" value="'+performednamaz[i]+'"/><a href="#" data-role="button" data-inline="true" data-icon="plus" id=button'+i+' style="float:right;width:30%;">add</a>' +''+ '</div>'; htmlstring += '</div>'; } $("#chart1").empty().append(htmlstring).trigger('create'); } } using code id of every button on click event:
$('button').live('click', function(){ var btnid = $(this).attr('id'); alert(btnid); }); something syntax mistake on line that's why not working else when try static assigning id working fine
htmlstring += '<div class="ui-block-c ui-bar-e" style="font-size:x-medium;height:80px;"><input type="number" data-inline="true" data-mini="true" value="'+performednamaz[i]+'"/><a href="#" data-role="button" data-inline="true" data-icon="plus" id=button'+i+' style="float:right;width:30%;">add</a>' +''+ '</div>';
problem
value='+performednamaz[i]+' , id=button'+i+' changed
htmlstring += '<div class="ui-block-c ui-bar-e" style="font-size:x-medium;height:80px;"><input type="number" data-inline="true" data-mini="true" value="+performednamaz[i]+"/><a href="#" data-role="button" data-inline="true" data-icon="plus" id=button"+i+" style="float:right;width:30%;">add</a>' and live() deprecated use on() function button click.
Comments
Post a Comment