SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding onClick on the fly

    Can someone help me out with this. I am trying to add an onclick event any and all anchor tags bases on whether they have a parent id of wrapper. The code is below. Thanks for any help!

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<title>Untitled Document</title>
    		<script type="text/javascript">
    			function start(){
    				var element1_links = document.getElementById('wrapper').getElementsByTagName('a');
             for (i=0; i < element1_links.length; i++) {
    
    				 		element1_links[i].onclick = function() {
                   test('variable');
                   
                }
             }
    			}
        </script>
    	</head>
    	<body onload="start()">
    		<div  id="wrapper">
    			<a href="#">This is a test</a><br />
    		</div>
    		
    	</body>
    </html>
    Last edited by matches; Nov 7, 2007 at 10:07.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could handle all of the clicks from the wrapper-

    Code:
    window.onload= function(){
    	document.getElementById('wrapper').onclick= function(e){
    		e= window.event.srcElement ||e.target;
    		if(e.href) alert(e.href);
    		return true;
    	}
    }

  3. #3
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    turns out the code I had above works fine for what I need it to. I was just not testing it properly . I do have a new problem that I am going to post on a new thread. Thanks for your response!


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
  •