javascript - How to get reference to current textbox in a table which contains 50 textboxes? -
basically have table 50 rows , each row has 2 td. 1 input , next 1 has label , dynamic.
refer image
dilemma user enters number in textbox(how know 1 if dont want loop through of them?) , on enter key event call javascript function checks if valid , adds corresponding message next tds label. how know reference of input without looping through textboxes call function on each textbox input's enter function?
the following works, based upon (limited available information), though requires handle validation yourself, obviously:
function yourdefinedfunction(){ // need handle validating yourself, somehow. return false; } /* binds 'keypress' 'tr' elements, 'listens' see if take place on/in 'td' element */ $('tbody tr').on('keypress', 'td', function(e){ /* 'this' 'td' element, 'e.target' element within 'td' received event var cell = this, target = e.target; // if key pressed enter key, *and* target input, then: if (e.which === 13 && target.tagname.tolowercase() == 'input') { // determine whether entry valid, want: var validity = yourdefinedfunction() || 'valid'; // , set text of next 'td' element reflect validity: $(cell).next('td').text(validity); } });
references:
Comments
Post a Comment