[javascript] How to get all elements in form

<html>
<head>
<script type="text/javascript">
	function showFormElement(){
		//Get all element from object form
		var els = document.getElementById('sys_form').elements;
		var str = "";
		for(var i = 0; i < els.length; i++){
			str += "type : "+ els[i].type +" name : "+ 
			els[i].name + " id : "+ els[i].id +" value : "+ 
			els[i].value + "\n";
		} 
		alert(str);
	}
</script>
</head>
<body>
<form id="sys_form" name="sys_form" target="#">
	<input type="hidden" name="el_hidden" id="el_hidden" value="I'm hidden" />
	<input type="text" name="el_text" id="el_text" value="I'm text" /><br />
	<select name="el_select" id="el_select">
		<option value="xxx">xxx</option>
	</select><br />
	<input type="checkbox" name="el_checkbox" id="checkbox" checked /><br />
	<input type="radio" name="el_radio" /><br />
	<textarea name="el_textarea" id="el_textarea">I'm textarea</textarea><br />
	<br />
	<input type="button" value="Show Info" onclick="showFormElement();" /> 
</form>
</body>
</html>

หมายเหตุ : ถ้าใช้ jQuery นี่คงสบายกว่านี้

  1. เจ๋งดีอ่ะ list มันออกให้หมด

  1. No trackbacks yet.