1

I have a link that has onclick attribute, which unfortunately I cannot modify. What I wanna do is to call and execute another function before onclick function is called. Upon researching I figured out I cannot do that as onClick has first priority.

In order to achieve what I want here is what I want to do. Using Jquery remove onclick attribute and then add modified onclick attribute as soon as page loads.

For example: I currently have this.

<a href="#" class='myclass' onclick="Joomla.submitbutton('article.apply')">link</a>

I want to make it look like this

<a href="#" class='myclass' onclick="MyFunction(); Joomla.submitbutton('article.apply')">link</a>

That way, it will execute myfunction before default function.

I am not good with Jquery, can someone help. Thanks.

2
  • where are you checking for the condition for removing and adding onclick function? javascript? codebehind? Commented Dec 2, 2013 at 21:04
  • @Krishna sorry I did not understand your question. Commented Dec 2, 2013 at 21:13

5 Answers 5

2

The attr() method sets or returns the attributes and values of the selected elements.

$('.myclass').attr('onclick', "myFunction()");
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure about cross-browser compatibility, but at least in Chrome, you can get a reference to the function to be called, remove the click handler, then bind your own:

http://jsfiddle.net/guXV3/

<a href="#" onclick="clickHandler();">click me</a>

function clickHandler() {
    console.log('inline handler');
}

$(document).ready(function () {
    $('a').each(function () {
        var originalHandler = $(this)[0].onclick;
        $(this).removeAttr('onclick');
        $(this).on('click', function () {
            console.log('new handler');
            originalHandler();
        });
    });

});

Clicking the link here logs new handler, then inline handler.

1 Comment

Thank You, I had to make some tweaks as per my need but this help a lot.
0

To add your handler.

$('.myclass').click(function(){
    //your function code goes here.
});

To remove the previous handler.

$('.myclass').attr('onclick', '');

Comments

0

Check the fiddle for full example.

$(function(){
   var $some_link = $('#some_link');
   var onClick = $some_link[0].onclick;
   $some_link.unbind('click', onClick);
   $some_link.attr('onclick', null);
   $some_link.on('click', function(e){ myBeforeCall(e); onClick(e); });
});

function myBeforeCall(){
   alert('Before');   
}

Comments

0

To add to the inline you can do this:

$(function () {
    $('.myclass').attr('onclick', function (i, attr) {        
        return 'MyFunction();' + attr;
    });
})

This will change the html to the way you have it shown and MyFunction() fires first

DEMO

1 Comment

this was helpful as well. But since there were more than one link to deal with I had to use other code. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.