I am dynamically creating a hyperlink in the c# code behind file of ASP.NET. I need to call a JavaScript function on client click. how do i accomplish this?
-
possible duplicate of [How To Use A Link To Call Javascript?](http://stackoverflow.com/questions/688196/how-to-use-a-link-to-call-javascript) – NoDataDumpNoContribution Jun 05 '14 at 16:31
9 Answers
Neater still, instead of the typical href="#"
or href="javascript:void"
or href="whatever"
, I think this makes much more sense:
var el = document.getElementById('foo');
el.onclick = showFoo;
function showFoo() {
alert('I am foo!');
return false;
}
<a href="no-javascript.html" title="Get some foo!" id="foo">Show me some foo</a>
If Javascript fails, there is some feedback. Furthermore, erratic behavior (page jumping in the case of href="#"
, visiting the same page in the case of href=""
) is eliminated.

- 3,429
- 6
- 45
- 76

- 339,989
- 67
- 413
- 406
-
15
-
-
@Chris - I'm not sure you get my point Chris. I'm suggesting that you don't use Do nothing and instead use Do something and then use progressive enhancement to override the href. This is the cleanest solution. Anchors are fine, but they should do SOMETHING if javascript is disabled - or for some reason (as per IE6) some javascript broke before the handler was created and assigned. This way all of your users are happy. – Lewis Aug 12 '09 at 14:49
-
2Not working for me, just automatically executes onclick function on page load. This also seems like a nightmare where accessibility is concerned. – Shawn Solomon Mar 23 '12 at 15:19
-
you are shall not use the "()" only the function name, currently I am trying to find out how to pass a param to the function, other wise, I might call a function first that sets the parameters globally...dunno yet – Wiston Coronell Jul 01 '14 at 17:07
-
4
-
@Coronellx If you would like to add parameters you could always do: `el.onclick = function() {return showFoo(param1, param2);};`. Now onclick points to a function, but you can still send in the desired parameters. – Pphoenix Sep 10 '15 at 14:20
-
For those, this didn't work shall try this: `function showFoo(e) { e.preventDefault(); alert('I am foo!'); return false; }` – Shubham Kushwah Sep 02 '18 at 08:26
The simplest answer of all is...
<a href="javascript:alert('You clicked!')">My link</a>
Or to answer the question of calling a javascript function:
<script type="text/javascript">
function myFunction(myMessage) {
alert(myMessage);
}
</script>
<a href="javascript:myFunction('You clicked!')">My link</a>

- 2,154
- 2
- 21
- 24
-
3The link in the StackOverflow 'Run code snippet' doesn't work in Firefox (no alert is created), but it works in FF when I put this type of link on a normal webpage. – the_pwner224 May 09 '19 at 20:01
-
Yes it's odd, it was working for me, now it isn't. [Others were having similar issues](https://stackoverflow.com/questions/20810794/alert-and-console-log-not-working-in-firefox-26) and there seems to be no one definitive solution, aside from completely reinstalling Firefox. – Jayden Lawson Sep 17 '19 at 05:33
With the onclick parameter...
<a href='http://www.google.com' onclick='myJavaScriptFunction();'>mylink</a>

- 4,750
- 2
- 29
- 22
-
2confusing statement. is it going to google.com or calling javascript function? which has the priority? – Nguai al Jan 14 '20 at 03:30
-
The JQuery answer. Since JavaScript was invented in order to develop JQuery, I am giving you an example in JQuery doing this:
<div class="menu">
<a href="http://example.org">Example</a>
<a href="http://foobar.com">Foobar.com</a>
</div>
<script>
jQuery( 'div.menu a' )
.click(function() {
do_the_click( this.href );
return false;
});
// play the funky music white boy
function do_the_click( url )
{
alert( url );
}
</script>

- 8,948
- 9
- 47
- 69
-
61
-
39This was like reading that Lego was invented to build Lego Batman. – Shawn Solomon Mar 23 '12 at 14:34
-
5
-
1
I prefer using the onclick method rather than the href for javascript hyperlinks. And always use alerts to determine what value do you have.
<a href='#' onclick='jsFunction();alert('it works!');'>Link</a>
It could be also used on input tags eg.
<input type='button' value='Submit' onclick='jsFunction();alert('it works!');'>

- 121
- 1
- 7
Ideally I would avoid generating links in you code behind altogether as your code will need recompiling every time you want to make a change to the 'markup' of each of those links. If you have to do it I would not embed your javascript 'calls' inside your HTML, it's a bad practice altogether, your markup should describe your document not what it does, thats the job of your javascript.
Use an approach where you have a specific id for each element (or class if its common functionality) and then use Progressive Enhancement to add the event handler(s), something like:
[c# example only probably not the way you're writing out your js]
Response.Write("<a href=\"/link/for/javascriptDisabled/Browsers.aspx\" id=\"uxAncMyLink\">My Link</a>");
[Javascript]
document.getElementById('uxAncMyLink').onclick = function(e){
// do some stuff here
return false;
}
That way your code won't break for users with JS disabled and it will have a clear seperation of concerns.
Hope that is of use.

- 5,769
- 6
- 30
- 40
Use the onclick
HTML attribute.
The
onclick
event handler captures a click event from the users’ mouse button on the element to which theonclick
attribute is applied. This action usually results in a call to a script method such as a JavaScript function [...]

- 110,061
- 20
- 134
- 146
I would generally recommend using element.attachEvent (IE) or element.addEventListener (other browsers) over setting the onclick event directly as the latter will replace any existing event handlers for that element.
attachEvent / addEventListening allow multiple event handlers to be created.

- 5,373
- 1
- 34
- 46
If you do not wait for the page to be loaded you will not be able to select the element by id. This solution should work for anyone having trouble getting the code to execute
<script type="text/javascript">
window.onload = function() {
document.getElementById("delete").onclick = function() {myFunction()};
function myFunction() {
//your code goes here
alert('Alert message here');
}
};
</script>
<a href='#' id='delete'>Delete Document</a>

- 3,459
- 6
- 28
- 42

- 36
- 4