user1556433
user1556433

Reputation:

How to use href and onclick on <a> tag simultaneously?

I want to use href as well as onclick one after another. I am trying like this, but my javascript function is not called? Is it the right approach to do so?

<a href="register.php" title="" onclick="javaScript:showRegisterRules();">Register</a>

function showRegisterRules()
{
 alert("Registration Rules");
}

How can I achieve my functionality?

Upvotes: 0

Views: 668

Answers (6)

Manwal
Manwal

Reputation: 23816

You can do this also

<a href="register.php" title="" onclick="return showRegisterRules('register.php');">Register</a>

function showRegisterRules(url)
{
 alert("Registration Rules");

 return true;
}

Upvotes: 1

penguinRaider
penguinRaider

Reputation: 33

Just one Question

Did you put the javascript in script tag ? Because once I did this it worked

<a href="register.php" title="" onclick="javaScript:showRegisterRules();">Register</a>

JS

<script>
    function showRegisterRules()
    {
     alert("Registration Rules");
    }

</script>

Upvotes: 2

bizzehdee
bizzehdee

Reputation: 21003

You can actually write a generic method here rather than one spessific to any given URL.

<a href="register.php" title="" onclick="showRegisterRules(this);">Register</a>

function showRegisterRules(a)
{
    alert("Registration Rules");
    document.location.href=a.href;
}

This way, you can reuse the same method anywhere within the site to show the alert. This allowing it to go in a script file rather than the html page

Upvotes: 1

underscore
underscore

Reputation: 6877

JS

<script type="text/javascript">
    function showRegisterRules()
    {
     alert("Registration Rules");
    }

</script>

HTML

 <a href="register.php" title="" onclick="showRegisterRules();">Register</a>

Upvotes: 2

frogatto
frogatto

Reputation: 29285

Try this:

<a href="#" title="" onclick="javaScript:showRegisterRules();window.location.href='register.php'">Register</a>

Upvotes: 1

sdespont
sdespont

Reputation: 14025

Something like:

<a href="javascript:void()" title="" onclick="javaScript:showRegisterRules('register.php');">Register</a>

function showRegisterRules(url)
{
 alert("Registration Rules");

 //Go to your url
 document.location.href=url;
}

Upvotes: 2

Related Questions