want to build a function inside a href link. should look like this
<a href="javacript:destroy();"></a>
How do i complete it correct?
want to build a function inside a href link. should look like this
<a href="javacript:destroy();"></a>
How do i complete it correct?
Use onclick
to trigger a function instead of href
HTML
<a href="#" onclick="destroy()"></a>
JS
function destroy()
{ //rest of the code
return false; // return false is use to prevent default behavior
}
Note: If you dont use return false
or event.preventDefault
it will execute the default behavior
There are different option for you to use. Most of them already answered in another question.
Sum it up:
You can do it like this (the correct way):
<a id="myLink" title="Click to do something" href="PleaseEnableJavascript.html" onclick="destroy();return false;">link text</a>
What it means is that after the destroy function is called it wont direct the user to the link provided. Alternatively you can use jQuery to make a listener on the specific a href like this:
$('#myLink').click(function(){
MyFunction();
return false;
});
The rest can be read on the provided link :)