javascript - Access value from datalist inside table -
i trying value of datalist within table cell. have function loops through each cell of table value:
function gettablecontents() { //gets table var otable = document.getelementbyid('projectstable'); var projectnames = []; //gets rows of table var rowlength = otable.rows.length; //loops through rows (i = 1; < rowlength; i++){ //gets cells of current row var ocells = otable.rows.item(i).cells; //gets amount of cells of current row var celllength = ocells.length; //push html content of each cell array projectnames.push(ocells.item(0).innerhtml); } //loop through each stored cell , check whether project exists in database for( = 0; < projectnames.length; i++){ checkuser(projectnames[i]); } }
the line gets value of cell returns html of datalist not actual value. can value different way using :
document.getelementbyid("listofprojects").value;
however undesirable table possibly contain more 1 datalist field using same datalist id. need way able value of datalist in single cell each cell containing datalist. appreciated cheers.
edit:
where first define datalist:
$('.addnew').click(function(){ "use strict"; $('#projectstable tbody').append("<tr class='child'> \ <td><input type='text' id='listofprojects' list='projectnames' placeholder='e.g. project name'> \ <datalist id='projectnames'> \ </datalist></td> \ <td> <input type='button' class='removerow' value='remove item'> </td></tr>");
where update datalist content:
function getprojectnames(){ //ensure function ran once. otherwise causes duplicates in datalist if(!projectnamesretrieved){ var datalist = document.getelementbyid('projectnames'); if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() { if (this.readystate == 4 && this.status == 200) { yourval = json.parse(this.responsetext); document.getelementbyid("projectsresults").innerhtml = yourval; for(var = 0; < yourval.length; i++) { var carsoption = "<option value=\"" + yourval[i].name + "\">" + yourval[i].name + "</option>"; $('#projectnames').append(carsoption); } } }; xmlhttp.open("get","getprojectnames.php",true); xmlhttp.setrequestheader("content-type", "application/json"); xmlhttp.send(null); projectnamesretrieved = true; } }
it if can post sample table data. because, .innerhtml should work extract value of cell. but, if not working may try using
projectnames.push(ocells.item(0).innertext);
even if doesn't help, please post test data, can see looking for.
Comments
Post a Comment