SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: DOM and events

  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Location
    Iran
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM and events

    I add objects to page by DOM methods but I can not change the events of new created objects.
    for example I add select boxes to my page by document.createElement('select') but i can not change the event handler of onchange to the function that i want.
    thanks

  2. #2
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <html>
    <head>
    	<title>Test</title>
    	<script type="text/javascript">
    		var newTag;
    		
    		function init() {
    			newTag = document.createElement('p');
    			newTag.appendChild(document.createTextNode('Click me!'));
    			newTag.onclick = function(e) {
    				alert('Thanks for that!');
    			}
    			document.body.appendChild(newTag);
    		}
    	</script>
    </head>
    <body onload="init();">
    	<h1>Test</h1>
    </body>
    </html>
    Cheers,
    D.

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Location
    Iran
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your answer. Is there any solution to assign the event as a string not as a new function reference.
    for example element.onchange="myfunction('myparameter')";

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    newTag.onclick = (function (p) {
        return function (e) {
            myfunction(p);
        };
    })(myparameter);
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •