0

I have 2 a links with different onclick values. They both share the same class.

The onclick values are these

BUTTON 1

onclick="return confirm('Click OK to cancel this booking.\n\nCancellation fees may be payable.\nSee the Terms and Conditions.');"

BUTTON 2

onclick="return confirm('Do you want to cancel this service?\nClick \'OK\' to continue. Click \'Cancel\' if you do not wish to proceed.');"

What I'm trying to do.

When someone click on the anchor link, ideally I want to find the return value and based on that change the confirm popup message to something else. So for the first button, new message will be message 1 and button 2 new message will be message 2.

The problem is, i can't edit the html as its a 3rd party program. I only can manipulate it with jquery. I tried this but Im kind of lost on how to identify which anchor link is clicked

var cancelBookingClick = $('a.replaced_btn')[0].onclick;
var newCancelBookingClick = cancelBookingClick.toString();

if (newCancelBookingClick.indexOf('Click OK to cancel this booking') > -1){
   //replace THIS button onclick event
}

This is what the A tag looks like

<a href="/view.php?booking=AIZLZ9&amp;cancel=t" class="replaced_btn" onclick="return confirm('Click OK to cancel this booking.\n\nCancellation fees may be payable.\nSee the Terms and Conditions.');" ></a>
5
  • Do the a elements have IDs? Commented Feb 29, 2016 at 0:45
  • No they done have ids. just classes Commented Feb 29, 2016 at 0:47
  • If you can't do it based on an nth index, then you'll have to do it with some other criteria. Without seeing the HTML of the page and the links, there's no way for us to tell you how, we can just maybe throw out ideas. Can they be identified by the href attribute? If yes, then write an if statement that tests for a word or words or path in the href. Commented Feb 29, 2016 at 0:47
  • I updated the question with how the A tag looks like Commented Feb 29, 2016 at 0:48
  • How do the <a> tags differ? do they have unique classes? please add into question the diff classes if they have one.. Commented Feb 29, 2016 at 0:52

3 Answers 3

1

If there's only those two links you could do this:

$('a.replaced_btn').eq(0).click(function(){
    confirm('Click OK to cancel this booking.\n\nCancellation fees may be payable.\nSee the Terms and Conditions.');
});

$('a.replaced_btn').eq(1).click(function(){
    confirm('Do you want to cancel this service?\nClick \'OK\' to continue.  Click \'Cancel\' if you do not wish to proceed.');
});

Edit:

var links = $('a.replaced_btn');
for(var l = 0; l < links.length; l++){
    if($(links[l]).attr('onclick').match("Click OK to cancel"))
        $(links[l]).attr('onclick', 'return confirm(some other message)');
    else //Do you want to cancel...
        $(links[l]).attr('onclick', 'return confirm(yet another message)');
}

This is a pretty odd way to solve this problem, but seems to be what you're asking for. Basically check the onclick attribute using .attr() and perform a match against the known values of the confirmation message. Then you can set the onclick attribute (also using .attr()) to something else.

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

4 Comments

Thanks. But there could be more. I cant target with n:th element sort of thing as I cannot guarantee its place on the DOM
Are the a tags part of a parent element of any kind? It would be nice to know what the HTML looks like that contains these links.
Updated the answer. You may be able to use .attr to get the text of onclick and perform an if-else based on its contents.
Thanks a lot. THIS WORKED!
0

The way I typically do something like this would be to define a custom data attribute on the links, and then listen for a click of any of the links. When the click event fires and my function runs, I can use the data attribute on the clicked link referenced in the event to do as I need.

I find event management this way is cleaner than using the onClick on the element itself.

Here's what that might look like:

<a href="#" class="replaced_btn" data-message="Click OK to cancel this booking.\n\nCancellation fees may be payable.\nSee the Terms and Conditions.">Cancel booking</a>
<a href="#" class="replaced_btn" data-message="Do you want to cancel this service?\nClick \'OK\' to continue.  Click \'Cancel\' if you do not wish to proceed.">Cancel service</a>

<script type="text/javascript">
    $("a.replaced_btn").on("click", function (e) {
        //Let the event bubble, but prevent the anchor from jumping to the top of the page
        e.preventDefault();

        //React to the specific link
        alert($(e.target).data("message"));
    });
</script>

Please note that code is untested.

Hopefully this helps resolve your issue.

Documentation for using jQuery's .data() method to get HTML5 data attributes: https://api.jquery.com/data/#data-html5

Documentation on getting the clicked-element from the jQuery event object: https://api.jquery.com/event.target/

Edit 1

You can also use an attribute to switch functionality, something I find super useful:

<a href="#" class="replaced_btn" data-type="cancel-booking">Cancel booking</a>
<a href="#" class="replaced_btn" data-type="cancel-service">Cancel service</a>

<script type="text/javascript">
    $("a.replaced_btn").on("click", function (e) {
        //Let the event bubble, but prevent the anchor from jumping to the top of the page
        e.preventDefault();

        //React to the specific link
        switch ($(e.target).data("type")) {
            case "cancel-booking":
                //Handle this case
                break;
            case "cancel-service":
                //Handle this case
                break;
        }
    });
</script>

Please note this code is also untested.

Edit 2

So if you use this snippet, it will add the data-type attributes for you, and then you can use that do whatever you want that requires identifying the link, including what I wrote above.

$("a.replaced_btn").each(function (idx, el) {
    if ($(el).attr("onclick").indexOf("Click OK to cancel this booking") > -1) {
        $(el).data("type", "cancel-booking");
    } else if ($(el).attr("onclick").indexOf("Do you want to cancel this service") > -1) {
        $(el).data("type", "cancel-service");
    }
});

Alternatively, skip the adding of a data-type attribute and handle you link-specific logic inside the function.

Edit 3

Here's a JSFiddle showing that you can loop through the links and perform separate functions on them depending on the contents of their onclick attribute:

https://jsfiddle.net/2xa7xk32/

(Also changed the second if in Edit 2 to be an else-if)

5 Comments

The problem is, i cannt edit the html as its a 3rd party program. I only can manimulate it with jquery
Hmmm, can use jQuery to update the links, so that you can then do something more useful with jQuery later. I'll try and update my answer with an example of what I mean.
If I can look for a specific word in the onclick event and replace its value thats it. But I cant figure out how to do it.
Yeah, you've got the right idea, I think. I'm putting together a jsfiddle now that appear to work.
Answer has been updated. The snippet I added is a way to churn through any links with the replaced_btn class and essentially pre-process them so you can then write any JS you might need that needs to identify them individually. If you're going to assign your own onclick's, I suggest removing the onclick that is already on the link: $(el).removeAttr("onclick");
0

If the href attribute is the only thing that you can depend on in the a elements to be unique, then that is what you'll need to use. Following is a simple example, but since we don't know what the other link looks like, or all of the other links on the page, it is impossible to know exactly what key off of in the URL. It will likely need to be changed to use a regular expression.

$("a").click(function(){
    if(this.href.indexOf('booking') != -1){
    alert("Booking link clicked")
  }
    if(this.href.indexOf('service') != -1){
    alert("Service link clicked")
  }
return false;
});

Also, you'll need to remove the existing click handlers with something like this first:

  $("a").each(function() {
    if (this.href.indexOf('booking') != -1 || this.href.indexOf('service') != -1) {
      $(this).attr("onclick", "").unbind("click");
    }
  });

Otherwise, the inline click handler and your click handler will both run.

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.