Ad

Submit Individual Form Element With Keypress

- 1 answer

I have a section to show all the answers to a related blog post with comment adding functionality to it

 <div id='answerwrapper'>
      <?php foreach($answerStuff as $answer){?>
     <div class='styleanswer'><?php echo $answer->answer_body;?></div>
     <a href='' class='cmnt'>comment</a>
     <form>
           <textarea class='answerComment' placeholder=' add a comment......' ></textarea>
     </form>
     <hr>
     <?php } ?>
</div>

There are multiple answers ,so there should be multiple form element with comment textfield.All i want is to hit enter key to submit each new comment.Problem is i can't figure out how to access specific form when working with specific textarea.I added this line to submit the form but it is not working

$(this).form.submit() 

All i could manage is the following:

var comment=$('.cmnt');
var textarea=$('.answerComment');
for(i=0;i<comment.length;i++){
   (function(i){
        $(comment[i]).click(function(e){
            e.preventDefault();
            $(textarea[i]).show();
            $(textarea[i]).keypress(function(e){
                if(e.which===13){
                  $(this).form.submit()   //does nothing, need to submit the particular form element from here
                } 
            });
            $(this).hide();
        });
    })(i);
}

can anyone help me with this problem? thanks :)

Ad

Answer

Textarea is the child of the form so: $(this).parent("form").submit();

You can use children() and parent() in jQuery to select children and parent elements.

Ad
source: stackoverflow.com
Ad