A href needs two javascript statements

They have: 1 posts

Joined: Aug 2009

Just a quick one.

I have an anchored list and need two events to happen when someone clicks on the link. Both of them involve href.

The first is for href="#1" to focus the user to the specified page area.
The second is to invoke a javascript function.

Code:

<ul>
<li><a href="#1""javascript:highlightCode('1')">TEXT TEXT</a></li>
</ul>

<div id="1">
</div>

I know anchors allow multiple javascript statements but i don't know how to use javascript to focus on a specific area in the document before the script is executed.

Thanks in advance

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Keep the #1 as the href (that is what it s really there for), and run actual javascript from the onclick attribute:

<a href="#1" onclick="highlightCode('1');">TEXT TEXT</a>

If you need multiple Javascript calls them selves, you can separate then with a semicolon, or create a new function that calls them both.

One added feature of using the onclick, it will check to see what value is returned by the javascript call. If it gets a "false" value, it will not process the href portion.

<a href="/regularlink.php" onclick="return FormNotUpdated();">TEXT TEXT</a>

If "FormNotUpdated" returns true it will follow the link, otherwise it will not go anywhere.

-Greg

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.