Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the updated post. In my original post, I asked to how to trigger a jQuery event. I got 2 solution responses. For an unknown reason, my original post was gone. I have to re-post it. In this update, I revised my coded per one of the response. The jquery function is triggered. However, I could not get my expected result. The related code is below. If I click the link on ht page, Outlook pops out. In this triggered event, Outlook does not pop out even though the jquery function is triggered. In this update, I wish someone can point out what I should do to make the triggered event like a real onclick. Thanks.
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestEmail.aspx.cs" Inherits="GIS_Webpages.RequestForm.TestEmail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    
        <p style="margin-removed 0px;" id='p1' >
            <input id="btn2" name="btn2" type="button" value="Submit" />
            <a href="#" class="aaf" id='a1'  >zzzzzzz</a>
         </p>
    
    </div>
    </form>

    <script type="text/javascript">
        $('.aaf').on("click", function () {
            debugger;
            var id_ = $(this).attr("id");
            var href = $(this).attr("href");
        })

        function f1() {
            debugger;
            document.getElementById('a1').href = "mailto:xxx@myCompany.com?subject=free chocolate&body=Hi! How are you?";
            $('#a1').trigger('click');
        }

        f1();

    </script>
</body>
</html>


What I have tried:

See the description above - Can't trigger the click event in tag
Posted
Updated 2-Mar-16 8:15am
v9

You are using class
class="aaf"

and class selector is preceded by a dot.
$('.aaf').click();

learn more: jQuery Selectors[^]
try this:
JavaScript
$('.aaf').on("click", function () {
   alert("U click me");
})

function f1() {
  $('.aaf').click();
}

f1();
 
Share this answer
 
v2
Try with below code:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>

$(document).ready(function(){

	$('.aaf').on("click", function () {
		alert('jsa');
	});
});

function f1() {
	alert('jsa111');
	$('#a1').trigger('click');
}
</script>
</head>
<body>
<p style="margin-removed 0px;" id='p1' >
    <input id="btn2" name="btn2" type="button" value="Submit" onclick="f1()" />
    <a href="#" class="aaf" id='a1'>xxxxxxx</a>
 </p>

</body>
</html>
 
Share this answer
 
v2
Comments
s yu 2-Mar-16 14:18pm    
MK: Thanks for your response. I revised my code (see my updated one in my post) per your advisory. It does trigger the event.
In this code, if I click the link directly, Outlook pops out. However, I could not get my expected result after I click the button that will trigger the jQuery function. Do you know what's wrong in my code? Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900