0

I am iterating through table which has the following structure

<tr val='question'>
    <td ><input style='width:500px' type='text' placeholder='Q.Enter your question here for radio button?'>
</tr>

I want to retrieve the value of the input box using Javascript

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
abhishek
  • 149
  • 12

3 Answers3

0
var value = document.querySelectorAll('input[type=text]').value;
var arr = [];
for(i = 0; i < value.length; i++)
    arr[i] = value[i];

and by this way you'll get an array with all of the values for the inputs with type text!

Khaled Al-Ansari
  • 3,910
  • 2
  • 24
  • 27
  • This is a bad solution because it will match the first text input on the page and get the value. It won't work on any page with more than one text input. querySelector itself could be fine, but it would be better to look for the element as a child of a specific container. Or give the element an ID (if possible with the structure) and select it that way. – calvinf Apr 19 '16 at 16:58
  • @calvinf better now? – Khaled Al-Ansari Apr 19 '16 at 17:02
-1

Add name and id attribute to your input element.

<tr>
    <td ><input style='width:500px' type='text' id='ab' name='ab' placeholder='Q.Enter your question here for radio button?'>
</tr>

You can get the value of input using your id

<script>
var qn = document.getElementById("ab").value;
</script>
Hanan Ashraf
  • 518
  • 3
  • 7
  • 21
-1
  1. give your <input> an id attribute

  2. theValue = document.getElementById('yourId').value;

That's it.

Éric Viala
  • 596
  • 2
  • 12