0

I have these Dynamic user inputs with following types:

  • Costs: <selection>The user will choose 1 option
  • Money Input: user input some text.I got after help.

I want Theses 2 values given above.

I want to get the Values of these Dynamic user inputs using JavaScript.

Here the Code given below : Update:solved

<!DOCTYPE html>
<html>
   <head>
      <style>
         table, th, td {
         border-collapse: collapse;
         }
      </style>
      <script>
         function addMore() {
             var table = document.getElementById("myTable");
             var row = table.insertRow(-1);
             var cell1 = row.insertCell(-1);
             var cell2 = row.insertCell(-1);
         
         var x = document.getElementById("myTable").rows[1].cells;
            
             cell1.innerHTML =  x[0].innerHTML;
             cell2.innerHTML = x[1].innerHTML;
         }
         
         
         function removeLast() {
             document.getElementById("myTable").deleteRow(-1);
         }
         
      </script>
   </head>
   <body>
      <form  action="payroll.php" method="post">
         <table id="myTable">
            <tr>
               <th>Costs</th>
               <th>Money Input</th>
            </tr>
            <tr>
               <td>
                  <select class="mySelect" name="DESCR" >
                     <option  disabled="" selected="">Select</option>
                     <option value="BASIC PAY">BASIC PAY</option>
                     <option  value="GAS BILL">GAS BILL</option>
                     <option  value="CLUB">CLUB</option>
                     <option  value="MEDICINE">MEDICINE</option>
                     <option  value="BANK LOAN">BANK LOAN</option>
                  </select>
               </td>
               <td> <input type="text" name="ALAMT"></td>
            </tr>
         </table>
         
      </form>
      <br>
      <button onclick="addMore()">Add More</button>
      <button onclick="removeLast()">Remove</button>
      <button onclick="myFunction()">Try it</button>
   </body>
   <script>
      function myFunction() {
      
var select = document.getElementById("myTable").rows[1].cells[0].firstElementChild;
  var selectValue = select.options[select.selectedIndex].value;
  var ocell = document.getElementById("myTable").rows[1].cells[1];
  alert("Selected Index: "+selectValue+"\nInput value: "+ocell.firstElementChild.value);
  
        
      }
   </script>
</html>

Using this code every i get errors.But i want the <option> value that is only chosen by the user, instead off all.

Please suggest me how can i do this or more better alternatives to do it. please let me know for any further information.Thanks

2 Answers 2

1

You get the HTMLTableCellElement object since you don't specify what you want to get from the cells[0], should add .textContent to if you want to get the text inside the column :

document.getElementById("myTable").rows[1].cells[0].textContent

Or .innerHTML if you want to get the HTML inside the column :

document.getElementById("myTable").rows[1].cells[0].innerHTML

If you want to get the selected option form the select by the user in 'Costs' select, i suggest to use classes .mySelect instead id="mySelect" since the id should be unique in same document, and use querySelector e.g :

var costs = document.querySelector('#myTable tr:nth-child(2) td:nth-child(1) .mySelect option:checked').value;
var money = document.querySelector('#myTable tr:nth-child(2) td:nth-child(2) input").value;

Hope this helps.


function addMore() {
  var table = document.getElementById("myTable");
  var row = table.insertRow(-1);
  var cell1 = row.insertCell(-1);
  var cell2 = row.insertCell(-1);

  var x = document.getElementById("myTable").rows[1].cells;

  cell1.innerHTML =  x[0].innerHTML;
  cell2.innerHTML = x[1].innerHTML;
}


function removeLast() {
  document.getElementById("myTable").deleteRow(-1);
}

function myFunction() {
  var costs = document.querySelector('#myTable tr:nth-child(2) td:nth-child(1) .mySelect option:checked').value;
  var money = document.querySelector('#myTable tr:nth-child(2) td:nth-child(2) input').value;  

  document.getElementById('result').textContent = costs+' - '+money;
}
table, th, td {
  border-collapse: collapse;
}
<form action="payroll.php" method="post">
  <table id="myTable">
    <tr>
      <th>Costs</th>
      <th>Money Input</th>
    </tr>
    <tr>
      <td>
        <select class="mySelect" name="DESCR" >
          <option  disabled="" selected="">Select</option>
          <option value="BASIC PAY">BASIC PAY</option>
          <option  value="GAS BILL">GAS BILL</option>
          <option  value="CLUB">CLUB</option>
          <option  value="MEDICINE">MEDICINE</option>
          <option  value="BANK LOAN">BANK LOAN</option>
        </select>
      </td>
      <td> <input type="text" name="ALAMT"></td>
    </tr>
  </table>

</form>
<br>
<button onclick="addMore()">Add More</button>
<button onclick="removeLast()">Remove</button>
<button onclick="myFunction()">Try it</button>
<br>
<span id='result'></span>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Zakaria Acharki for reply.But I want only the selected option form the select by the user in 'Costs' Drop Down and the user inserted value in the 'Money Input' Field.Please suggest me.Thanks.
Thanks @Zakaria Acharki for reply.But still it is not showing user given inputs only.Thanks.
Glad i can help, it's my fault, should use nth-child instead of eq.
1
  var select = document.getElementById("myTable").rows[1].cells[0].firstElementChild;
  var selectValue = select.options[select.selectedIndex].value;
  var ocell = document.getElementById("myTable").rows[1].cells[1];
  alert("Selected Index: "+selectValue+"\nInput value: "+ocell.firstElementChild.value);

8 Comments

Thanks @Sunil B N for the reply. But i actually want to take input that is only chosen by the user from the given list for Costs field and input value inserted by the user in Money Value field.Thanks again.
ocell.firstElementChild.value gives Money value field
Thanks @Sunil B N for the reply.it worked for the 2nd one.How about the 1st input [the list].Do you have any idea.Thanks.
var select = document.getElementById("myTable").rows[1].cells[0].firstElementChild; var selectValue = select.options[select.selectedIndex].value;
Brother, you seriously need to learn some programming basics and come back here. NO OFFENSE intended.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.