Is it possible to hyperlink JavaScript array elements into an HTML table? -


this code have done far. want hyperlink websites when array outputted table in html, websites clickable , link respective webpages. reason, code in type="text/javascript" different code in language="javascript" , have no idea why. if provide code language="javascript" appreciated!

html:

<table id="table">   <tr id="tbody">     <th>mattress type</th>     <th>link</th>   </tr> </table> 

javascript:

<script language="javascript">  var table = document.getelementbyid("table"); var body = document.createelement("tbody");  var beds = new array(3); beds[0] = ["spring mattress", "king size", "http://factorymattresstexas.com/specials/spring-air/"]; beds[1] = ["rest lumbar support", "queen size", "http://factorymattresstexas.com/specials/beautyrest-lumbar-support"]; beds[2] = ["beauty rest", "twin size", "http://factorymattresstexas.com/specials/simmons-beautyrest/"];  table.appendchild(tbody); beds.foreach(function(items) {   var row = document.createelement("tr");   items.foreach(function(item) {     var cell = document.createelement("td");     cell.textcontent = item;     row.appendchild(cell);   });   table.appendchild(row); }); </script> 

you're there. need continue adding child elements:

var table = document.getelementbyid("table");  var body = document.createelement("tbody");    // initialize empty array  var beds = [];    // add bed objects array  beds.push({    type: "spring mattress",    size: "king size",    link: "http://factorymattresstexas.com/specials/spring-air/"  });  beds.push({    type: "rest lumbar support",    size: "queen size",    link: "http://factorymattresstexas.com/specials/beautyrest-lumbar-support"  });  beds.push({    type: "beauty rest",    size: "twin size",    link: "http://factorymattresstexas.com/specials/simmons-beautyrest/"  });    table.appendchild(tbody);  beds.foreach(function(item) {    var row = document.createelement("tr");      // dumping whole array contents in cell.  want have separate cells each type of information.    var type = document.createelement("td");    type.textcontent = item.type;      var size = document.createelement("td");    size.textcontent = item.size;      // create containing cell hold link    var link_td = document.createelement("td");      // create <a href="...">...</a> element    var link = document.createelement("a");    link.textcontent = item.link;    link.href = item.link      // add link cell    link_td.appendchild(link);      // add cells row in order you'd see them in    row.appendchild(type);    row.appendchild(size);    row.appendchild(link);      table.appendchild(row);  });
<table id="table">    <tr id="tbody">      <th>mattress type</th>      <th>size</th>      <th>link</th>    </tr>  </table>

update: beds array array of array of strings. switched use array of bed objects. allows define properties , reference properties name instead of index (ie item.size vs item[1]). cleaner , scale better codebase grows. can extend bed object additional properties want display.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -