Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Jquery + onclick

  • 25-05-2011 06:42AM
    #1
    Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭


    I am trying to retrieve all onclick events related to a <a href>
    some of them are hardcoded and had events added dynamically

    e.g. <a href="XX" onclick="runX():">

    now we have a 3rd party which add a load of tracking info to the onclick event to any 'A' tag
    e.g.
    $(document).ready(function(){
    	//add onclick for every link with XX in it
    	$.each($("a[href*='XX']"), function(i, element){
    		$(this).click(function() { 
    			alert('dynamic onclick');
    		});
    	});
    });
    

    Now when I try to retrieve all the onclick functions
    I can only get returned the hardcoded event rather than the 'dynamic' onclick event.

    I'm guessing I have to loop through each attr of the element var?
    But I'm not really sure if that is correct

    I have attached an example of what i am doing - rename the txt file to html

    Any help at all - cheers

    P.s. this is a simplified version of what is happening


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 9,220 Mod ✭✭✭✭mewso


    The problem here is that jQuery will use more modern methods to add events to elements. There is a discussion of it here - http://stackoverflow.com/questions/446892/how-to-find-event-listeners-on-a-dom-node - and the jQuery method given in the second answer should do what you want:-
            $("a[href*='XX']").click(function (i, element) {
                var onc = $(this).attr("onclick");
                alert("The inline onclick value is " + onc);
    
                var clickEvents = $(this).data("events").click;
                jQuery.each(clickEvents, function (key, value) {
                    alert("One of the DOM onclick events is " + value.handler) // alerts "function() { alert('clicked!') }"
                })
    
            });
    


  • Registered Users, Registered Users 2 Posts: 12,026 ✭✭✭✭Giblet


    Use event delegation instead of adding lots of click events to the DOM.
    $('#SomeContainer').delegate('a','click',function(e){
        e.preventDefault();
        //DoWork
    });
    

    You could then add a class to the link instead of changing the click event to make the delegate target that element instead.
    <div id="SomeContainer">
    <a name="XX"></a>
    
      <a href="#XX">Click here</a>
    </div>
     <script>
    	$('#SomeContainer').delegate('a:not(.UpdatedClick)','click',function(e){
    		e.preventDefault();
    		alert('1');
    	});
    	$('#SomeContainer').delegate('.UpdatedClick','click',function(e){
    		e.preventDefault();
    		alert('2');
    	});
    	$('a').addClass('UpdatedClick');
      </script>
    


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    mewso wrote: »
    The problem here is that jQuery will use more modern methods to add events to elements. There is a discussion of it here - http://stackoverflow.com/questions/446892/how-to-find-event-listeners-on-a-dom-node - and the jQuery method given in the second answer should do what you want:-
            $("a[href*='XX']").click(function (i, element) {
                var onc = $(this).attr("onclick");
                alert("The inline onclick value is " + onc);
    
                [B]var clickEvents = $(this).data("events").click;
                jQuery.each(clickEvents, function (key, value)[/B] {
                    alert("One of the DOM onclick events is " + value.handler) // alerts "function() { alert('clicked!') }"
                })
    
            });
    

    Thanks, that is exactly what I was looking for.

    and cheers for the link


Advertisement