SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add function for onclick

    I was trying to add a function to an onclick event without using addeventlistener ...


    Code:
    function addOnclick(){
    document.getElementById('one').onclick+=function(){alert('goobye')}
    }
    does not work

    any ideas? can it be done like that

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can it be done like that
    No. You can do this though:
    Code:
    var elmt = document.getElementById("one");
    oldfunc = elmt.onclick;
    elmt.onclick = function(){oldfunc(); alert('goodbye')};

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will do it:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    
    	<head>
    		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    		<title>Untitled Page</title>
    		<script type="text/javascript"><!--
    function addOnclick(id,func) {
    	myEl = document.getElementById(id);
    	if (typeof myEl.onclick == "function") {
    		oldFunction = myEl.onclick;
    		myEl.onclick = function() {
    			oldFunction();
    			func();
    		}
    	} else {
    		myEl.onclick = func;
    	}
    }
    
    function defaultFunc() {
    	alert("This is the default function.");
    }
    
    function newFunc() {
    	alert("This is the NEW function.");
    }
    
    window.onload = function() {
    	addOnclick("btn",newFunc)
    };
    
    /* or you can define functinos on the fly like this:
    
    	addOnclick("btn",function() {
    		alert("asdasdasd");
    	})
    
    */
    //-->
    </script>
    	</head>
    
    	<body>
    		<button id="btn" type="button" onclick="defaultFunc()";>Button</button>
    	</body>
    
    </html>

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud View Post
    No. You can do this though:
    Code:
    var elmt = document.getElementById("one");
    oldfunc = elmt.onclick;
    elmt.onclick = function(){oldfunc(); alert('goodbye')};

    That will fail if the element does not have onclick already set, as "oldfunc" will not be an object.

  5. #5
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gee I would never have though to rebuild the function ...
    Tell me what are you running on, I want the same stuff !!!

    Thanks !

  6. #6
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well we are nearly there but my proble is taht I am working on an a tag ...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    
    	<head>
    		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
    		<title>Untitled Page</title>
    		<script type="text/javascript">
    function addOnclick(id,func) {
    	var myEl = document.getElementById(id);
    	if ( typeof myEl.onclick== 'function') {
    		oldFunction = myEl.onclick;
    		myEl.onclick = function() {
    				
    			func();
    			oldFunction();
    		}
    	} else {
    		myEl.onclick = func;
    	}
    }
    
    function popme(myurl){
    window.open(myurl)
    }
    
    </script>
    	</head>
    
    	<body onload="addOnclick('btn',function(){alert('ajout');})">
    		<a href="http://www.google.fr" id="btn" onclick="this.href='#';popme('http://www.google.com');return false;" ;>Button</a>
    	</body>
    
    </html>
    Expected action on mother page just have the alert, but instead I have the alert and the the redirection ... I only want the popup...

    Note I had to change chronology of
    func();
    oldFunction();

    because of return false existing in onclick ...

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note I had to change chronology of
    func();
    oldFunction();

    because of return false existing in onclick ...
    Change the order of those two functions, execute the code, and then post up about whether this statement is true:
    Note I had to change chronology...because of return false existing in onclick

  8. #8
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I see what you mean the return false only affects the running function.. but I ha them the other way round and it doesn't work any better ...

    So the problem does nopt come from there ...


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
  •