-2

This is my form

<button (click)="M_add()">Add</button>
<tbody id="_tbody">
</tbody>

When the add button is clicked, it will create an input field

var tbody = document.getElementById("_tbody");
var row = document.createElement("tr");
var col = document.createElement("td");
var col_index = document.createElement("input");
col_index.setAttribute("class", "index");
col_index.setAttribute("id", "index");
row.appendChild(col_index);
tbody.appendChild(row);

I need to retrieve value from each input field, how to get it?

3
  • You can only have 1 id per page. Append a number to your id instead like 'index_' + ctr. Commented Jul 23, 2018 at 2:06
  • how to add that one ? and how to retrieve their value? Commented Jul 23, 2018 at 2:28
  • I posted an answer but it's not using the approach above Commented Jul 23, 2018 at 2:56

2 Answers 2

0

you can do it by angular text() function

$('#id').text()
Sign up to request clarification or add additional context in comments.

2 Comments

by using this i only get first value
you can use $scope to store count variable. $scope.count = 0; in M_add function, increase this by 1. $scope.count++; then add it in id of element col_index.setAttribute("id", "index_"+$scope.count+"");
0

I can only answer in AngularJS but I think the concept is still there.

$scope.table = [
  { value: 1 },
  { value: 2}
];
$scope.getRowValue = (row) => {
  $scope.selectedRow = row;
};
$scope.addTableRow = () => {
  $scope.table.push({
    value: $scope.table.length + 1
  });
};

This approach uses an array of the things that you want to display in the table. Every time you add a new row, it is appended to the array.

<table border="1" width="100%">
  <tbody>
    <tr ng-repeat="row in table">
      <td>
        <input type="text" value="{{row.value}}" ng-model="row.model" ng-init="row.model=row.value">
      </td>
      <td>
        <button ng-click="getRowValue(row.model)">Get Row Value</button>
      </td>
    </tr>
  </tbody>
</table>
<button ng-click="addTableRow()">Add Table Row</button>

In the HTML, the array is displayed in ng-repeat. Each instance of ng-model is a reference to an object in the array. Read more here

So basically what happens when user clicks on the button it will get the model of the input in that row and pass it to somewhere.

Check the working fiddle

Comments

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.