Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I want to execute click event of an anchor tag by javascript or jscript.
Is there any way to do ?
i tried with every code but it seems to be not working.


Here is my html code:
<br />
<html xmlns="http://www.w3.org/1999/xhtml"><br />
<head runat="server"><br />
    <title></title><br />
    <script type="text/javascript"><br />
        function dothis() {<br />
            alert('I want this');<br />
            document.getElementById('about').click();<br />
<br />
                    }<br />
    </script></pre><br />
<br />
<body><br />
    <form id="form1" runat="server"><br />
    <a href="AboutUs.aspx" name="about" id="about" >click here</a><br />
    </form><br />
</body><br />
</html><br />




Javascript code:
function dothis() {
alert('I want this');

document.getElementById('about').click();
}

and this above javascript code is getting executed while page load in C#
I am able to execute function dothis() on page load
and alert msg gets displayed but click event of anchor tag is not executed ..there i need help
Thanks in advance for help
Posted
Updated 17-Jul-14 18:02pm
v2
Comments
Sergey Alexandrovich Kryukov 17-Jul-14 13:27pm    
This is not "HTML code"... And you are not even trying to handle click event. Where?!
You don't mention dothis() anywhere else.
—SA
HellHotie 17-Jul-14 23:21pm    
"This is not "HTML code"..."
That was a typo actual html code is below
i cannot write anchor tag here as it is not displaying so i am replacing '<' with '('
(a href=".aspx" name="about" id="about")click here(/a)

"And you are not even trying to handle click event. Where?!
You don't mention dothis() anywhere else."

That click event is handled by code behind page in C#
when that aspx page load that click event is called..by C# code and it executes javascript function.. and my C# code is perfectly calling that function and alert box is getting displayed. bt click event is not executing.. there i need help
Sergey Alexandrovich Kryukov 17-Jul-14 23:45pm    
Look, you have something which does not work; and you don't know why. That's why I cannot trust any of your statements, only your code, and only obtained by proper Paste operation from the code you really tried. Also, instead of adding code to comment, click Improve question and do it.
—SA
HellHotie 18-Jul-14 0:05am    
Does that question helps you to understand problem?
Sergey Alexandrovich Kryukov 18-Jul-14 0:03am    
Wait a second... You say "i cannot write anchor tag here as it is not displaying..."...

Look, the here is some HTML tag: <div />. By some strange reason, I can write this HTML fragment in the post (which is HTML content), and you kinda not...
All you need to know is how to write simple HTML. Sorry, if you cannot write simple static HTML just to show your sample, how can you possibly can develop in ASP.NET, Javascript? I would advise you to learn just the basics of HTML and then come back. This is a Quick Questions & Answers forums. Asking questions also has its prerequisites...

—SA

1 solution

Hi dear,

Here i have pasted the code for achieving your goal.
For this we need to include the jQuery library file to out HTML.

Ex :-
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <script src="jquery-2.0.3.js"></script>
    <script type="text/javascript">
        $(function () {
            dothis();
        });

        function dothis() {
            alert('I want this');
            $('#about')[0].click();
        }
    </script>
</head>

<body>
    <form id="form1"  runat="server">
        <div>
            <a href="AboutUs.aspx" id="about">click here</a>
        </div>
    </form>
</body>
</html>


Hoping this will be of help to you, please have a look into it let me know if it worked for you.
So that i can help you more on this regard if required.
 
Share this answer
 
v2
Comments
HellHotie 18-Jul-14 1:02am    
Thanxx thats what i was looking for i was stuck into it from quite a time
Jscript did that trick. but is same possible by javascript as well?
How to make javascript function fire click event of an anchor tag?
SRS(The Coder) 18-Jul-14 1:12am    
You are always welcome but the below line should work for expected result :-

document.getElementById('about').click();

It worked for ne not sure if there is any issue with your side, can you pelase let me know which browser version are you using. Again you can open the Developers tool for the browser you are using by pressing F12 and in 'Console' tab you can watch if any error it is throwing, which will help you to solve the issue if any there.
HellHotie 18-Jul-14 1:34am    
Ok thnx again i got desired result by javascript as well. (thumbs up)
SRS(The Coder) 18-Jul-14 1:35am    
wl cm alwys :-)

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