Denoteone
Denoteone

Reputation: 4055

add an anchor and onClick to a link

I have an image with a link that when clicked I would like the user to be brought to an anchor on the same page and then the cursor to be focused on the input directly below.

<a onclick="document.newsletter.email.focus();" href="#newssection"><img src="images/newsletter_slide.jpg" /></a>

When it is just the onClick it works but does not scroll the page down and when I use the anchor it goes to that section but does not focus.

I switched around the onClick and the href and it didnt help

Upvotes: 0

Views: 1785

Answers (2)

Kyle
Kyle

Reputation: 22278

Try this.

http://jsfiddle.net/HgSjH/3/

<a onclick="document.newsletter.email.focus();return false;" href="#">click me</a>
<form name="newsletter" id="newssection">
  <input id="email" name="email"/>
</form>

Upvotes: 5

Starx
Starx

Reputation: 79041

I think a simple

document.getElementById('yourEmailField').focus(); should do the trick.

It will automatically focus to the element, and if needed scroll too.

Upvotes: 0

Related Questions