I have some code that when a user clicks on a link “Add Comment”, it looks a certain named div next to it and slidetoggles down that div. Inside of that div I have a textarea.
My goal is to set the focus on the textarea when the user clicks the “Add Comment” link.
The problem I’m running into is I will have multiple comments/textareas/add comment links and I need it so when you click on “Add Comment” it focuses on the corresponding textarea.
Heres what I have so far:
jQuery
$(".add-comment-btn").click(function(){
$(this).next(".inline-comment-form").slideToggle(200);
$(this).next(".inline-comment-form textarea").focus();
return false;
});
HTML
<a class="add-comment-btn" href="#">Add Comment</a>
<div class="inline-comment-form">
<form action="" method="post">
<div class="textarea">
<textarea class="inline-comment-textarea" name="comment-textarea"></textarea>
</div>
<input value="SUBMIT" name="submit-inline-comment" class="form-btn" type="submit">
</form>
</div><!--/Inline Comment Form -->
Edit: I updated the code to add the Add Comment link and moved the return false
http://jsfiddle.net/Frgxv/
nextonly works with siblings, the textarea isn’t a sibling.