28

When I click a button I want the textarea in this li element to focus.

<li class="commentBlock" id="commentbox-79" style="display: list-item;"> <div> <div class="grid userImageBlockS"> <div class="imgSmall"> <img width="35" height="35" alt="image" src="/bakasura1/files/images/small/1288170363aca595cabb50.jpg"> </div> </div> <div class="grid userContentBlockS alpha omega"> <form accept-charset="utf-8" action="/bakasura1/account/saveComment" method="post"> <div style="display: none;"> <input type="hidden" value="POST" name="_method"> </div> <input type="hidden" id="StatusMessageReplyPid" value="79" name="data[StatusMessageReply][pid]"> <input type="hidden" id="StatusMessageReplyItemId" value="1" name="data[StatusMessageReply][item_id]"> <input type="hidden" id="StatusMessageReplyCommentersItemId" value="1" name="data[StatusMessageReply][commenters_item_id]"> <textarea id="StatusMessageReplyMessage" name="data[StatusMessageReply][message]"> Write your comment... </textarea> <input type="submit" name="" value="Comment" class="comment" id="comment-79"> </form> </div> <div class="clear"></div> </div> </li> 

This is my jQuery code:

$('.user-status-buttons').click(function() { var id = $(this).attr('id'); $("#commentbox-"+id).slideToggle("fast"); $("#commentbox-"+id+" #StatusMessageMessage").focus(); return false; }); 
2
  • 3
    You usually do not want to combine 2 id selectors in the same expression, this is bad practice as ids are meant to be unique throughout the DOM. Commented Nov 16, 2010 at 4:36
  • @Jacob on the button i only have the number but on the li element i have comment-id. shouldnt that help ? Commented Nov 16, 2010 at 4:43

3 Answers 3

36

I use timer to focus text areas :

setTimeout(function() { $el.find('textarea').focus(); }, 0); 
Sign up to request clarification or add additional context in comments.

2 Comments

It works with this solution. Can you explain why its not working without it?
As I understand it, the click itself will grant something focus during the event handling, overriding your own focus. The timeout executes your focus after the current synchronous event handling has completed.
28

Based on your comment in reply to Jacob, perhaps you want:

$('.user-status-buttons').click(function(){ var id = $(this).attr('id'); $("#commentbox-"+id).slideToggle("fast", function(){ $("#commentbox-"+id+" #StatusMessageReplyMessage").focus(); }); return false; }); 

This should give the #StatusMessageReplyMessage element focus after the slide effect has finished.

1 Comment

thanks. i had written the wrong text area id :D StatusMessageReplyMessage should have been instead of StatusMessageMessage
25

The easiest solution is to use jQuery focus

 $('#StatusMessageReplyMessage').focus(); 

NOTE: if you are testing this in the console, Chrome will send the focus back to the console! This can lead you to believe it had not worked when in fact it works perfectly. Just be aware of other focus grabbing scripts/behavior in your environment and it will all be fine :)

1 Comment

This solution is great. Very straight to the point. I'm working with a private password protected web app that lives inside an iframe and every time the page reloads, the textarea loses autofocus and it's frustrating to have to keep clicking in the textarea to type again. This solved it. Take my upvote. I put it in the same document that the textarea resides in so that when it refreshes, it hits that js focus line and immediately snaps the cursor where it should be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.