Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Jquery + onclick

  • 25-05-2011 5: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,035 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: 11,989 ✭✭✭✭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