Passing Variables To onChange Event with jQuery .change() -


i'm writing simple web application , have hit stumbling block of sorts - i've found workaround, i'd understand i'm doing wrong having scoured jquery documentation.

i have function, when called page, inserts table row:

var rowcount = 0; function addrow(tbody){     $("#"+tbody).append(         $('<tr>').attr('id','row_'+rowcount).append(             $('<td>').append('content'),             $('<td>').append($('<input>').addclass('text').attr('readonly','readonly')),             $('<td>').append($('<input>').addclass('text').attr('readonly','readonly')),             $('<td>').attr('id','barcode_cell'+rowcount),             $('<td>').append($('<input>').addclass('text').addclass('quantity').attr('maxlength',3)),             $('<td>').append($('<select>').change(function(){ismissing(this,rowcount)}))                     )     ); } 

where concerned final <select> box , it's onchange event. ismissing function:

function ismissing(elem, counter){   alert(elem.value);   alert(counter); } 

what happens selected value of box alerts expected, however, row number always alerts latest row, regardless of row calling it. instead want hold value of rowcount variable when row introduced, not latest value of variable after have added additional rows.

what doing wrong? how force variable remain static? have use global variable there more 1 function adds rows more 1 table, way can ensure each row in table has unique identifier.

this hapens because rowcount in global scope. can make inside addrow method:

function addrow(tbody){     var internalvariable = rowcount;     $("#"+tbody).append(         $('<tr>').attr('id','row_'+rowcount).append(             $('<td>').append('content'),             $('<td>').append($('<input>').addclass('text').attr('readonly','readonly')),             $('<td>').append($('<input>').addclass('text').attr('readonly','readonly')),             $('<td>').attr('id','barcode_cell'+rowcount),             $('<td>').append($('<input>').addclass('text').addclass('quantity').attr('maxlength',3)),             $('<td>').append($('<select>').change(function(){ismissing(this,internalvariable)}))                 ) ); 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -