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; });
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.
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.
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.
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.
I use timer to focus text areas :
setTimeout(function() { $el.find('textarea').focus(); }, 0);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With