Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 29 people (48%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

How do I assign an onchange handler to a node created via DOM methods?

Apr 5th, 2008 20:32
ha mo, sven vahar, Dan,


This works with IE & NS6+. Remeber, onchange fires when element loses
focus so you have to move focus from the input to see whoa() in action.
<html>
<head>
	<title> * </title>
	<script language="JavaScript" type="text/javascript">
	<!--
	function init(){
		var x = document.getElementsByTagName('body')[0];
		var y = document.createElement('form');
		var z = document.createElement('input');
		z.setAttribute('name','foo');
		z.onchange = whoa;
		y.appendChild(z);
		x.appendChild(y);
	};
	function whoa (){
		alert('yea, you changed me');
	};
	// -->
	</script>
</head>
<body onload="init()">
</body>
</html>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com