admin管理员组文章数量:1183717
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!
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!
4 Answers
Reset to default 20If 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;
You could use firstChild.value
like this:
var x = document.getElementById("tabela").rows[2].cells[3].firstChild.value;
Demo
If you can provide id to your input element,
HTML
<td><input type="text" id="text1"/></td>
JS
var x = document.getElementById("text1").value;
You can use querySelector()
DOM method:
document.querySelector('#tabela tr:nth-child(2) td:nth-child(3) > input').value
JSFiddle
本文标签: htmlHow can I get the value of an input in a specific table cell using javascriptStack Overflow
版权声明:本文标题:html - How can I get the value of an input in a specific table cell using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738235944a2070690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
class
orid
attribute to your input field? – Lix Commented Jun 19, 2014 at 13:06var x = document.getElementById("tabela").rows[2].cells[3].getElementsByTagName('input')[0].value
– W.D. Commented Jun 19, 2014 at 13:06innerHTML
would give you the input tag. But to actually get its value, you need to access, the tag itself - there are answers on this page that show how to do that. Getting the HTML for it is not useful, as you are better of getting the DOM node and manipulate that. – VLAZ Commented Jun 19, 2014 at 13:15