Jump to content

Focus on anchor using Javascript


deadimp

Recommended Posts

It's a basic question, but: What's the best way to have javascript focus on a link without adding the identifier 'clause' to the end of an url?

Example:

index.html

Random html...
<a id='anchor' href='...'>Anchor</a>
More random html

To have a browser focus on the anchor via the url, you just add #anchor to your url.

 

This is what I've come up with:

<script language='javascript'>
function anchor_focus(name) {
var obj=document.getElementById(name);
obj.focus();
obj.blur();
}
//Example
anchor_focus("anchor");
</script>

That scrolls it into view, and of course deselects it to mimick appending "#anchor" to the url.

It's worked on Firefox, IE, and Opera (all Windows).

 

Any other suggestions?

Link to comment
https://forums.phpfreaks.com/topic/70983-focus-on-anchor-using-javascript/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.