javascript - Form submit giving Undefined value -


i have table generating , has sort button. want column user wants sort keep getting undefined value in sort when try value "bycolumn": appreciated:

function createtable(data){             var str = "<form id='tableselect' action='javascript:void(0);'><table><thead><tr> <th>ticketnum</th><th>recieved</th><th>sendername</th><th>sender email</th><th>subject</th><th>tech</th><th>status</th><th>select</th></tr></thead><tbody>";              for(var key in data){                 if (!data.hasownproperty(key)) continue;                 var row = data[key];                 str += "<tr> <td>";                 str += row['ticketnum'] + "</td><td>";                 str += row['recieved'] + "</td><td>";                 str += row['sendername'] + "</td><td>";                 str += row['senderemail'] + "</td><td>";                 str += row['subject'] + "</td><td>";                 str += row['tech'] + "</td><td>";                 str += row['status'] + "</td><td>";                 str += "<input type='radio' name ='selectrow' value=" +row['ticketnum'] + ">" + "</td></tr>";             }             str += "<tr><td> sort by: <input type = 'radio' name = 'bycolumn' value='ticketnum'><td> sort by: <input type = 'radio' name = 'bycolumn' value='recieved'><td> sort by: <input type = 'radio' name = 'bycolumn' value='sendername'><td> sort by: <input type = 'radio' name = 'bycolumn' value='senderemail'><td> sort by: <input type = 'radio' name = 'bycolumn' value='subject'><td> sort by: <input type = 'radio' name = 'bycolumn' value='tech'><td> sort by: <input type = 'radio' name = 'bycolumn' value='status'><td> <button type ='button' value='submit' button class=\"mybutton\" onclick=\"sort();\">sort</button> </tr>";              str += "</tbody></table></form>";             console.log(str);             document.getelementbyid("table").innerhtml = str;         }     function sort(){         var table = currenttable;         var sortby = document. getelementsbyname("bycolumn").value; //the error on line         alert(sortby);      } 

enter image description here

you need use .getelementsbyname("bycolumn") inputs name='bycolumn' not .getelementbyid("bycolumn"). loop on radio's .checked .value

var data = [{  'ticketnum':'ticketnum',  'recieved':'recieved',  'sendername':'sendername',  'senderemail':'senderemail',  'subject':'subject',  'tech':'tech',  'status':'status'  },{  'ticketnum':'ticketnum',  'recieved':'recieved',  'sendername':'sendername',  'senderemail':'senderemail',  'subject':'subject',  'tech':'tech',  'status':'status'  },{  'ticketnum':'ticketnum',  'recieved':'recieved',  'sendername':'sendername',  'senderemail':'senderemail',  'subject':'subject',  'tech':'tech',  'status':'status'  }];  createtable(data);      function createtable(data) {        var str = "<form id='tableselect' action='javascript:void(0);'><table><thead><tr> <th>ticketnum</th><th>recieved</th><th>sendername</th><th>sender email</th><th>subject</th><th>tech</th><th>status</th><th>select</th></tr></thead><tbody>";              (var key in data) {          if (!data.hasownproperty(key)) continue;          var row = data[key];          str += "<tr> <td>";          str += row['ticketnum'] + "</td><td>";          str += row['recieved'] + "</td><td>";          str += row['sendername'] + "</td><td>";          str += row['senderemail'] + "</td><td>";          str += row['subject'] + "</td><td>";          str += row['tech'] + "</td><td>";          str += row['status'] + "</td><td>";          str += "<input type='radio' name ='selectrow' value=" + row['ticketnum'] + ">" + "</td></tr>";        }        str += "<tr><td> sort by: <input type = 'radio' name = 'bycolumn' value='ticketnum'><td> sort by: <input type = 'radio' name = 'bycolumn' value='recieved'><td> sort by: <input type = 'radio' name = 'bycolumn' value='sendername'><td> sort by: <input type = 'radio' name = 'bycolumn' value='senderemail'><td> sort by: <input type = 'radio' name = 'bycolumn' value='subject'><td> sort by: <input type = 'radio' name = 'bycolumn' value='tech'><td> sort by: <input type = 'radio' name = 'bycolumn' value='status'><td> <button type ='button' value='submit' button class=\"mybutton\" onclick=\"sort();\">sort</button> </tr>";              str += "</tbody></table></form>";        //console.log(str);        document.getelementbyid("table").innerhtml = str;      }            function sort() {        //var table = currenttable;        var col = document.getelementsbyname("bycolumn"); //the error on line        var sortby;        (var = 0; < col.length; i++) {          if (col[i].checked) {            sortby = col[i].value;            break;          }        }        alert("sort by: "+sortby);            }
<div id='table'></div>


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -