title: How to populate form input fields from table data using java script?
body: <p>I'm trying to populate from fields from table data but I really have no idea how to do it. I'm newbie in Java script. Kindly help me through this. Here is my code snapshot which shows add and delete record functionality through js. Thanks in advance!</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;link type="text/css" rel="stylesheet" href="css/bootstrap-theme.css"&gt;
      &lt;link type="text/css" rel="stylesheet" href="css/bootstrap.css"&gt;
      &lt;link type="text/css" rel="stylesheet" href="css/bootstrap-theme.min.css"&gt;
      &lt;link type="text/css" rel="stylesheet" href="fonts/glyphicons-halflings-regular.eot"&gt;
      &lt;link type="text/css" rel="stylesheet" href="js/bootstrap.js"&gt;
      &lt;link type="text/css" rel="stylesheet" href="js/bootstrap.min.js"&gt;
      &lt;script&gt;
          localStorage.setVisit = "";
          function first_visit(){
              if(typeof (localStorage.setVisit) == "undefined" || localStorage.setVisit == ""){
                  localStorage.setVisit = "Yes";
                  document.getElementById("up_btn").disabled = true;
                  document.getElementById("add_btn").disabled = false;
              }
          }
          function add_data(){
              var name_tb = document.getElementById("name").value;
              if(document.getElementById("Male").checked){
                  var gender = document.getElementById("Male").value;
              }else if(document.getElementById("Female").checked){
                  gender = document.getElementById("Female").value;
              }else{
                  gender = "";
              }
              var age_tb = document.getElementById("age").value;
              var city = document.getElementById("city");
              var city_selected = city.options[city.selectedIndex].value;
              var table = document.getElementById("table_person");
              var row = table.insertRow(-1);
  
              var par1 = row.insertCell(0);
              var par2 = row.insertCell(1);
              var par3 = row.insertCell(2);
              var par4 = row.insertCell(3);
              var par5 = row.insertCell(4);
              var par6 = row.insertCell(5);
  
              par1.innerHTML = name_tb;
              par2.innerHTML = gender;
              par3.innerHTML = age_tb;
              par4.innerHTML = city_selected;
              par5.innerHTML = '&lt;a href="#" onclick="update_rec();"&gt;Update&lt;/a&gt;';
              par6.innerHTML = '&lt;a href="#" onclick="delete_rec(this)"&gt;Delete&lt;/a&gt;';
              document.getElementById("up_btn").disabled = false;
              document.getElementById("add_btn").disabled = false;
          }
  
          function delete_rec(record){
              var del = record.parentNode.parentNode;
                  del.parentNode.removeChild(del);
              document.getElementById("add_btn").disabled = false;
          }
  
          function update_rec(record){
              document.getElementById("up_btn").disabled = false;
              document.getElementById("add_btn").disabled = true;
  
  
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="first_visit()"&gt;
  &lt;div class="container"&gt;
      &lt;div class="row" style="margin-top:2%;"&gt;
          &lt;div class="col-lg-8"&gt;
              &lt;h3&gt;List of Persons&lt;/h3&gt;
              &lt;table class="table table-striped" id="table_person" style="border: 2px solid black;"&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Gender&lt;/th&gt;
                      &lt;th&gt;Age&lt;/th&gt;
                      &lt;th&gt;City&lt;/th&gt;
                      &lt;th class="text-right"&gt;Action&lt;/th&gt;
                      &lt;th&gt;&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/div&gt;
          &lt;div class="col-lg-4"&gt;
              &lt;h3&gt;Add / Update Person Form&lt;/h3&gt;
              &lt;div style="border: 2px solid darkgrey; height: 55%;"&gt;
                  &lt;form class="form-horizontal" style="margin-top: 8%;"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;label class="control-label col-lg-3"&gt;Name:&lt;/label&gt;
                          &lt;div class="col-lg-8"&gt;
                          &lt;input id="name" type="text" name="name" class="form-control" placeholder="Name"
                                 style="border: 2px solid black;"&gt;
                      &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group" style="padding-top: 2%;"&gt;
                  &lt;label class="control-label col-lg-3"&gt;Gender:&lt;/label&gt;
                  &lt;div class="col-lg-8" style="margin-top: 2%;"&gt;
                      &lt;input type="radio" name="gender" value="Male" id="Male"&gt;Male
                      &lt;input type="radio" name="gender" value="Female" id="Female"&gt;Female
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group" style="padding-top: 2%;"&gt;
                  &lt;label class="control-label col-lg-3"&gt;Age:&lt;/label&gt;
                  &lt;div class="col-lg-8"&gt;
                      &lt;input id= "age" type="text" name="age" class="form-control" placeholder="Age"
                             style="border: 2px solid black;"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group" style="padding-top: 3%;"&gt;
                  &lt;label class="control-label col-lg-3"&gt;City:&lt;/label&gt;
                  &lt;div class="col-lg-8"&gt;
                      &lt;select id = "city" class="col-lg-12" style="margin-top: 1%; height: 10%; border: 2px solid black;"&gt;
                          &lt;option&gt;Lahore&lt;/option&gt;
                          &lt;option&gt;Karachi&lt;/option&gt;
                          &lt;option&gt;Islamabad&lt;/option&gt;
                          &lt;option&gt;Faisalabad&lt;/option&gt;
                          &lt;option&gt;Multan&lt;/option&gt;
                      &lt;/select&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group" style="padding-top: 3%;"&gt;
                  &lt;div class="col-lg-offset-2"&gt;
                      &lt;input type="reset" class="btn btn-default" value="Reset"
                             style="border:2px solid black;padding:1% 8% 1% 8%;"&gt;
                      &lt;input type="button" id="add_btn" onclick="add_data()" class="btn btn-default" value="Add"
                             style="border: 2px solid black;padding:1% 8% 1% 8%;"&gt;
                      &lt;input type="button" id="up_btn" class="btn btn-default" value="Update"
                             style="border: 2px solid black;padding:1% 8% 1% 8%;"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>