javascript - How to add an UpperCase function to each textbox in a dynamic table? -


i trying create dynamic table textboxes want textboxes converted upper case every time write.

any ideas on how this??

currently how doing dynamic table:

var n = 1; function addrow(tableid,nrocolumna) {     var table = document.getelementbyid(tableid);     var rowcount = table.rows.length;     var row = table.insertrow(rowcount);      for(i=0;i<nrocolumna;i++){          var cell = row.insertcell(i);         var element = document.createelement("input");         element.type = "text";         element.name = n+"0"+i;         element.size = "12";         element.id = n+"0"+i;          //element.onkeyup = function(){alert()};         cell.appendchild(element);     }     n++; } 

i trying document.getelementbyid(element.id).value.touppercase() getting error null value element.id

any appreciated!

i tested code onkeyup function activated:

var n = 1; function addrow(tableid,nrocolumna) {     var table = document.getelementbyid(tableid);     var rowcount = table.rows.length;     var row = table.insertrow(rowcount);      for(i=0;i<nrocolumna;i++){          var cell = row.insertcell(i);         var element = document.createelement("input");         element.type = "text";         element.name = n+"0"+i;         element.size = "12";         element.id = n+"0"+i;          element.onkeyup = function(){alert(element.id);};         cell.appendchild(element);     }     n++; } 

and worked. however, uses last element.id computed every call function... so, when created 1 row of 3 cells, every time typed cell, alert "102" regardless of cell typed in.

this because onkeyup function dynamic. called on keyup action - not set when object created. uses element.id value exists @ time of action, not when passed in first time. hope makes sense.

i had issue myself on recent project. 1 solution create separate function inner workings of loop such:

var n = 1;  function createrow (n, i) {     var element = document.createelement("input");     element.type = "text";     element.name = n+"0"+i;     element.size = "12";     element.id = n+"0"+i;      element.onkeyup = function(){alert(element.id);};      return element; } function addrow(tableid,nrocolumna) {     var table = document.getelementbyid(tableid);     var rowcount = table.rows.length;     var row = table.insertrow(rowcount);      for(i=0;i<nrocolumna;i++){         var cell = row.insertcell(i);         element = createrow(n, i);         cell.appendchild(element);     }     n++; } 

this code alerts correct element.id value.

edit: can change onkeyup() line read:

element.onkeyup = function(){document.getelementbyid(element.id).value = document.getelementbyid(element.id).value.touppercase();}; 

and should work want to.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -