javascript - copy input from one table to another table on same document -
please want use javascript copy input 1 table table on same document. there 2 tables. in first table, user input desired value. these values used column headers in 2nd table. far not getting script. please experts know doing wrong.
<!doctype html> <html> <head> <title>prtqual</title> <style type="text/css"> body {font-family: arial, verdana, helvetica,sans-serif; padding: 0px; margin-left: 0px; margin-left: 50px; max-width: 800px;} p, table {margin-left: 25px;} th {font-weight: bold; font-size: 14px; border: 1px; border-style: solid; margin: 0px; border-colapse: colapse; border-spacing: 0px;} td {font-weight: normal; font-size: 14px; border: 1px; border-style: solid; margin: 0px; border-colapse: colapse; border-spacing: 0px;} .hide {display: none;} .show {display: display;} .hove: hover {background-color: rgb(250,250,150);} .btn {padding: 5px 5px 5px 5px; background-color: rgb(240,240,240); margin- left: 25px; text-align: center; font-size: 12px; font-weight: bold; border: 1px; } .btn:hover {background-color: rgb(250,250,150)} </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> </head> <body> <table id="main"> <tbody> <tr class="hide"> <td id="ddnfmat"><input type="text" class="hove"/></td> </tr> <tr> <td>prtqual</td> </tr> <tr> <td>index</td> </tr> <tr> <td id="ddn"><input type="text" class="hove" /></td> </tr> </tbody> </table> <p><i> add new header, click on button below</i></p> <input class="btn" type="button" onclick="addnewheader()" value="add new factor"> <br><br> <input class="btn" type="button" onclick="copyheader()" value="finish"> <br><br> <table id="exp"> <thead> <tr id="exphdr"> <th>s/n</th> <th>prtqual</th> <th>index</th> <th id="ddno"><input type="text" readonly /></th> <th id="ddnt"><input type="text" readonly /></th> <th id="ddnth"><input type="text" readonly /></th> <th>comment</th> </tr> </thead> <tbody> </tbody> </table> <script> function addnewheader() { var table = document.getelementbyid('main'); var elmnt = table.getelementsbytagname("tr")[0]; var cln = elmnt.clonenode(true); table.tbodies[0].appendchild(cln); var p = document.getelementbyid('main').rows.length; var q = p - 1; var s = document.getelementbyid('main').rows[q]; s.setattribute("class", "show"); } </script> <script> function copyheader() { (i=3, i<6, i++) { tableheader(i) } } </script> <script> function tableheader(i){ var tble = document.getelementbyid('main'); var mytr = tble.getelementbytagname('tr')[i]; var mytd = mytr.getelementbytagname('td')[0]; var myinput = mytd.getelementbytagname('input')[0]; var myval = myinput.value; var tbl = document.getelementbyid('exp'); var hdr = tbl.getelementbytagname('tr')[0]; var hddata = hdr.getelementbytagname('td')[i]; var inp = hddata.getelementbytagname('input')[0]; inp.value = myval; } </script> </body> </html>
as m69 mentioned there wrong method names. after loop defined incorrectly. after changes code didn't error out except using wrong selector. in second table you're trying select , operate on <th>
not <td>
why "hddata" undefined.
<script> function addnewheader() { var table = document.getelementbyid('main'); var elmnt = table.getelementsbytagname("tr")[0]; var cln = elmnt.clonenode(true); table.tbodies[0].appendchild(cln); var p = document.getelementbyid('main').rows.length; var q = p - 1; var s = document.getelementbyid('main').rows[q]; s.setattribute("class", "show"); } function copyheader() { ( var = 3 ; < 6 ; i++) { // loop using comma separators in place of semicolons tableheader(i) } } function tableheader(i){ var tble = document.getelementbyid('main'); var mytr = tble.getelementsbytagname('tr')[i]; // these method calls misspelled var mytd = mytr.getelementsbytagname('td')[0]; var myinput = mytd.getelementsbytagname('input')[0]; var myval = myinput.value; var tbl = document.getelementbyid('exp'); var hdr = tbl.getelementsbytagname('tr')[0]; var hddata = hdr.getelementsbytagname('th')[i]; // select th tags here not tds! var inp = hddata.getelementsbytagname('input')[0]; inp.value = myval; } </script>
Comments
Post a Comment