jQuery script doesn't work in Wordpress -
i know problem has been asked alot of times can't jquery work in wordpress.
i have form going used wp_ajax before point wanted ensure interact jquery.
the form html placed on page , looks this:
<div id = "content"> <form action= "" method= "post" id= "hfwpform"> <label for= "fname">first name</label></br> <input type= "text" name= "fname" id= "fname" required></br> <label for= "email">email address</label></br> <input type= "email" name= "email" id= "email" required></br> <label for= "message">your message</label></br> <textarea name= "message" id= "message"></textarea> <input type= "hidden" name= "action" value= "contact_form"> <input type= "submit" value= "send"> </form> </br></br> <div id= "feedback">my feedback</div> </br></br> </div>
i have created plugin i'm using main php. in plugin i've created enqueue lines.
add_action( 'wp_enqueue_scripts', 'load_scripts'); function load_scripts() { if ( !is_admin() ) { wp_register_script('hftest', get_template_directory_uri() . '/js/hf_test.js', array('jquery'), '1.0', false ); wp_enqueue_script('hftest'); } }
i've created subfolder scripts called "js" , script should execute when submit form looks this.
(function($) { $(document).ready(function() { $('#hfwpform').submit(function() { $response = "this it!!!"; alert("submitted"); $("#feedback").text($response); }); }); })( jquery );
when submit form page reloads clearing input fields instead of changing feedback text , displaying alert box.
what doing wrong ???
regards flemming
your form functioning properly. missing specify in js form not execute via default functionality.
your code should this:
(function($) { $(document).ready(function() { $('#hfwpform').submit(function(e) { e.preventdefault(); //this key $response = "this it!!!"; alert("submitted"); $("#feedback").text($response); }); }); })( jquery );
Comments
Post a Comment