Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Textarea focus

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; }); 
like image 735
Harsha M V Avatar asked Nov 16 '10 04:11

Harsha M V


People also ask

How to focus on modal jQuery?

Answer: Use the jQuery . focus() method You can simply use the jQuery . focus() method to set the focus on the first input box or textarea inside the Bootstrap modal when it is loaded upon activation.

How to set focus on input field using jQuery?

Using jQuery With jQuery, you can use the . focus() method to trigger the “focus” JavaScript event on an element. This method is a shortcut for . trigger("focus") method.

What is focus function in jQuery?

jQuery focus() Method The focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often used together with the blur() method.

How do you focus text input?

Input Text focus() MethodThe focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.


2 Answers

I use timer to focus text areas :

setTimeout(function() {  $el.find('textarea').focus(); }, 0); 
like image 80
Roman Yudin Avatar answered Sep 21 '22 13:09

Roman Yudin


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.

like image 45
Horatio Alderaan Avatar answered Sep 25 '22 13:09

Horatio Alderaan