admin管理员组文章数量:1277888
Probably a dumb question, but is there a way in JavaScript to find out a form element's type (text, checkbox, etc.) from a form element after finding it by name or id? Either in plain old JavaScript or using jQuery-style syntax or methods would be ideal.
Example:
<input type="text" name="my_text_input" id="my_text_input"/>
<script>var element = document.getElementById('my_text_input'); // now how to get the element type?</script>
I'm not looking for alternatives, this is exactly what I want to do. I'm making a sort of screen scraper that needs to collect this information.
Probably a dumb question, but is there a way in JavaScript to find out a form element's type (text, checkbox, etc.) from a form element after finding it by name or id? Either in plain old JavaScript or using jQuery-style syntax or methods would be ideal.
Example:
<input type="text" name="my_text_input" id="my_text_input"/>
<script>var element = document.getElementById('my_text_input'); // now how to get the element type?</script>
I'm not looking for alternatives, this is exactly what I want to do. I'm making a sort of screen scraper that needs to collect this information.
Share Improve this question asked May 12, 2011 at 4:27 user187702user1877024 Answers
Reset to default 6The type
property will give you this...
document.getElementById('my_text_input').type;
However, you have also tagged the question with jQuery, so you could use...
$('#my_text_input').attr('type');
jsFiddle.
$('#form_id input').each(function() {
console.log($(this).attr('type'));
});
$('#my_text_input').attr('type');
Similary you can get any propery....class,title or any
$('#my_text_input').attr('class');
//retrieves classname
try this :
<!DOCTYPE html>
<html>
<body>
<script language="JavaScript" type="text/javascript">
function submit_form()
{
var len = document.form1.elements.length;
for(var i = 0 ; i< len;i++){
if(document.form1.elements[i].type == "text")
alert(document.form1.elements[i].value);
}
}
</script>
<form name="form1" onsubmit="submit_form();">
First name: <input type="text" name="FirstName" value="Mickey"><br>
First name: <input type="textarea" name="FirstName" value="Mickey"><br>
Second name: <input type="text" name="SecondName" value="Mickey"><br>
Last name: <input type="text" name="LastName" value="Mouse"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
本文标签: javascriptFinding a form element39s type after finding it by name or idStack Overflow
版权声明:本文标题:javascript - Finding a form element's type after finding it by name or id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741276267a2369745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论