-4

want to build a function inside a href link. should look like this

<a href="javacript:destroy();"></a>

How do i complete it correct?

cais
  • 13
  • 5

2 Answers2

0

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

brk
  • 48,835
  • 10
  • 56
  • 78
0

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 :)

Community
  • 1
  • 1
jonaslagoni
  • 663
  • 7
  • 23