1

I have the following link in html:

<a class="link_class" href="somelink.com">Link</a>

Without editing the HTML, I want to add an onclick event so it'll look like this:

<a class="link_class" onclick="return gtag_report_conversion('somelink.com');" href="somelink.com">Link</a>

How can I do this with jQuery?

2
  • 1
    Why not just unobtrusively add the event handler and then call gtag_report_conversion() from that? Commented May 14, 2019 at 11:22
  • api.jquery.com/on Commented May 14, 2019 at 11:22

3 Answers 3

3

To select all anchor elements or "link_class" elements and add a you can use:

    $("a.link_class").on("click", function(){
        //your code here
    });

(to select all links use "a". To select only .link_class elements use "link_class". The above code will select only links with class link_class)

You want this to run when the document is ready, so you wrap it on:

    $(document).ready(function(){
        $("a.link_class").on("click", function(){
            //your code here
        });
    });

(this probably already exists within your application, just make sure you put the code that selects the links inside the document.ready callback)

Again, this will attach the event to all items that match the selector. If you only want a specific item, give it an id, and use that to select it.

    $("#a-specific-id").on("click", function(){
        //your code here
    });

Alternatively, if you can't give it an ID, and only want to target a specific item, you could try the eq method like:

    $("a.link_class").eq(INDEX).on("click", function(){
        //your code here
    });

Where INDEX is the index of the specific link in the collection of links returned by the selector.

I think you will benefit from spending some time at the jQuery learning center: https://learn.jquery.com/

Sign up to request clarification or add additional context in comments.

Comments

2

Add with attr

$('.link_class').attr("onclick", "return gtag_report_conversion('somelink.com');")

https://jsfiddle.net/lalji1051/gu14dryt/4/

1 Comment

Is there a way to add this so it's reflected in the html code? so if I open source-code I actually see it as part of the code, as in my original post?
0

Combining the previously posted answers, and in case someone wants to track a specific conversion, and target ALL the links that contain herf="tel:", you can try the following:

$(document).ready(function(){ 
  $("a[href^='tel']").attr("onclick", "return gtag_report_conversion('tel:12345678');")
});

Also If you are using a CMS like WordPress, and you can add jQuery, you have to replace $ with jQuery. So the code will be something like this:

jQuery(document).ready(function(){
 jQuery("a[href^='tel']").attr("onclick", "return gtag_report_conversion('tel:12345678');")
});

Comments

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.