javascript - How can I pass the row id from a table with a button -


i have table want add button each row , edit row in form. need pass linenumber getjson request. how can that? here table , getjson. (edit)i noticed i'm asking linenumber want id of button pushed '+value.linenumber+'.

//table goes though loop. table_obj.append($('<tr><td>'+value.sln+'</td><td  >'+value.type+'</td><td     > '+value.stopnumber+'</td><td>'+value.loname+'</td><td >'+value.referenceno+'</td> <td class="hide" >'+value.typeid+'</td><td class="hide" >'+value.locationid+'</td> <td class="hide" >'+value.linenumber+'</td><td><button id='+value.linenumber+' onclick=edit()>edit</button></td></tr>'));  //getjson <script> function edit() $.getjson("editstops.php",{ linenumber:$('#linenumber').val() },stophandler) var stophandler = function(data){ $("#slns").val(data[0].value.loadnumber); $("#type").val(data[0].value.type);     $("#stopnumber").val(data[0].value.stopnumber); $("#loname").val(data[0].value.loname); $("#referenceno").val(data[0].value.referenceno);    $("#typeid").val(data[0].value.typeid); $("#locationid").val(data[0].value.locationid);     $("#linenumber").val(data[0].value.linenumber); };  </script> 

in button tag, have line number specified id attribute. can pass edit() function argument:

<button id='+value.linenumber+' onclick="edit('+value.linenumber+')">edit</button></td></tr> 

then edit function can accept argument , use it:

function edit(linenumber) {     $.getjson("editstops.php",{ linenumber: linenumber }, stophandler); } 

note if buttons inside form tag might need add return false; after calling edit() prevent submitting form , refreshing page.

simple working example: http://jsbin.com/okesom/2/edit

alternatively leave off onclick event in button tag entirely. after loop done , table built, attach event buttons this:

$("button").click(function() {     var linenumber = $(this).attr('id');     $.getjson("editstops.php",{ linenumber: linenumber }, stophandler); }); 

here quick example of second option: http://jsbin.com/ibokon/2/edit


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -