Roman
Roman

Reputation: 37

Jquery - Having problems with .prev()

If user clicks on button why form doesn't submit? I tried using .prev().prev() but it doesn't works.

HTML

 <form id="my_form" action"pr.php">
   <input type="text" name="cf"><span class="save_btn">Submit</span>
 </form>

JQUERY

$(".save_btn").click(function(){
            // Trim data
            var trm = $.trim($(this).prev().val());
            // Check length of data
            if(trm.length == 0){
                alert("Field can't be empty");
            } else{
                $(".save_btn").prev().prev().submit();
            }
        });

Upvotes: 1

Views: 36

Answers (2)

CosX
CosX

Reputation: 2030

Try .parent() This finds the element parent and in your case, the form element.

Upvotes: 2

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

you can use closest

$(this).closest('#my_form').submit();

Upvotes: 0

Related Questions