Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the value of an input in a specific table cell using javascript?

I'm wondering how can I get the value of an input in a specific table cell using javascript?

<td><input type="text"/></td>

I assume getting the innerHTML of a specific cell is quite simple, for example:

var x = document.getElementById("tabela").rows[2].cells[3].innerHTML

but this gives me just the input without it's value. Adding .value to the end of the line doesn't work. I would appreciate your help!

like image 710
Casi Avatar asked Jun 19 '14 13:06

Casi


3 Answers

If you don't have any id on the element you are after, then you could get the first child of the td by:

var x = document.getElementById("tabela").rows[n].cells[n].children[0].value;

Or if you want the first child to be specific to input then:

var x = document.getElementById("tabela").rows[n].cells[n].getElementsByTagName('input')[0].value;
like image 97
Abhitalks Avatar answered Nov 11 '22 00:11

Abhitalks


You could use firstChild.value like this:

var x = document.getElementById("tabela").rows[2].cells[3].firstChild.value;

Demo

like image 34
naota Avatar answered Nov 10 '22 22:11

naota


If you can provide id to your input element,

HTML

<td><input type="text" id="text1"/></td>

JS

var x = document.getElementById("text1").value;
like image 1
Mustafa sabir Avatar answered Nov 11 '22 00:11

Mustafa sabir