javascript - Onclick cell change value with Multiple Dynamic Tables -


jsfiddle.net file

$(document).ready($(function change_cell_content() {             $("td").on("click",                 function(){                     var $cell_id_ref = $(this).attr('cellid');                     var $table_id_ref = $(this).parent().parent().parent().attr('tableid');                     var newvalue;                      var currentvalue = document.queryselector('[cellid="' + $cell_id_ref + '"]').innerhtml;                     alert(currentvalue);                      var changevalue = prompt("enter new value. (current " + currentvalue + ")");                     if ( changevalue!==null ) {                         newvalue = changevalue;                         document.queryselector('[cellid="' + $cell_id_ref + '"]').innerhtml = newvalue;                     }                 });         })); 

the first table works perfectly, other 3 not.

click on cell. alert show tableid of clicked on. prompt popup , ask new value(stating current value in prompt). problem, currentvalue variable getting first table though using cellid($cell_id_ref) , tableid($table_id_ref) attempt direct other tables.

any or comments appreciated.

you can simplify code , make use of jquery instead of queryselectorall:

$(function change_cell_content() {     $("td").on("click", function () {          var cell_id_ref = $(this).attr('cellid');         var $table = $(this).closest('table');         var newvalue;          var currentvalue = $(this).html();         alert(currentvalue);          var changevalue = prompt("enter new value. (current " + currentvalue + ")");         if (changevalue !== null) {             $table.find('[cellid="' + cell_id_ref + '"]').text(changevalue);         }     }); }); 

you problem in line:

document.queryselector('[cellid="' + $cell_id_ref + '"]').innerhtml = ... 

you should select cell in current table, above code selects first cell first table.

demo: http://jsfiddle.net/wah5s/3/


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -