Jakub Hořínek
Jakub Hořínek

Reputation: 23

event.preventDefault() not working for submit on Wordpress

I am writing wordpress plugin.
I have some form jQuery script and I need use event.preventDefault(); (in a shortcode) for that. But event.preventDefault(); is not working for submit button, but it´s working, for example, for a link.

Can anyone help. Thanks in advance.

In shortcode:

echo '<a href="#" class="test" id="test_1">Some text</a>';
echo '<a href="#" class="test" id="test_2">Some text</a>';
echo '<a href="#" class="test" id="test_3">Some text</a>';  

echo '<form action=" " class="answerform" method="POST" >';

echo '<input  id=' . $answerid .' class=".submitform" type="submit"  value=' . $answer->answer . '>';
                    echo '</form>';

Script:

jQuery(function($){ 
$(document).ready(function() {
    $( ".submitform" ).click(function( event ) {
      event.preventDefault();
    });

    $( ".test" ).click(function( event ) {
      event.preventDefault();
    });
}); 

});

Upvotes: 2

Views: 2088

Answers (1)

David Wilkinson
David Wilkinson

Reputation: 5118

You've prepended your submit input element class with a . (ie - class=".submitform" when it should be class="submitform")

It should be, simply:

echo '<input  id=' . $answerid .' class="submitform" type="submit"  value=' . $answer->answer . '>';
echo '</form>';

At the moment, your script is looking for an element with a class of submitform, which doesn't exist (your input element's class is currently .submitform).

Upvotes: 2

Related Questions