0

I have an ajax call and at the end of it I'm setting href for anchor tag and want to click it but am unable to do so. Here is my code:

<div>
    <a id="hypSendEmail">email</a>
</div>

$("body").on("click", "#btnPopulateEmails", function () {
        var $form = $("#btnPopulateEmails").parents('form');
        $.ajax({
            type: "POST",
            url: url,
            async: true,
            data: $form.serialize()
        }).done(function (data) {
            var json = data,
            obj = JSON.parse(json);
            if (obj.MessageType == "success") {
                $("#hypSendEmail").attr('href', obj.MessageLink);
                $("#hypSendEmail").trigger("click");
                //$("#hypSendEmail").click();
                $.colorbox.close();
            }
            if (obj.MessageContent != null) {
                //do something else
            }
        });
        return false;
    });

I believe I have to somehow use .on function to trigger that click maybe? Please help. My purpose is to populate hyperlink with mailto & click it automatically. I tried doing this on code behing using Process.Start() & Response.Redirect(), but both failed, so now I'm trying my luck here. It does populate href tag on my anchor tag, but just doesn't click it.

Roco CTZ
  • 1,107
  • 1
  • 16
  • 31
DAK
  • 1,395
  • 4
  • 22
  • 35

2 Answers2

0

Looks like a possible duplicate of Jquery how to trigger click event on href element.

Jquery trigger is only triggering handlers and not the native behavior of the anchor tag. Instead of using jquery.trigger, use the native DOM "click" instead.

Change this line:

$("#hypSendEmail").trigger("click");

To this instead:

$("#hypSendEmail")[0].click();

Hope that helps!

Community
  • 1
  • 1
MatthewG
  • 8,583
  • 2
  • 25
  • 27
0

This worked thanks to Alexander & Ragnar comments:

window.location.href = obj.MessageLink;
DAK
  • 1,395
  • 4
  • 22
  • 35