0

I know this problem has been asked alot of times but I just can't get jQuery to work in Wordpress.

I have a form which is going to be used with some wp_ajax but before I get to this point I wanted to ensure that it could interact with jQuery.

The Form HTML is placed on the page and looks like 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 a plugin which I'm using for my main php. In this plugin I've created the 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 a subfolder for my scripts called "js" and the script that should execute when I submit the form looks like this.

(function($) {
   $(document).ready(function() {
      $('#hfwpform').submit(function() {
         $response = "This is it!!!";
         alert("Submitted");
         $("#feedback").text($response);
      });
   });
 })( jQuery );

When I submit the form the page reloads clearing the input fields instead of changing the feedback text and displaying an Alert box.

What am I doing wrong ???

Regards Flemming

2
  • Have you checked to make sure the script is getting called? Any errors in your dev console? Also you're putting a document ready function inside of a shorthand document ready function and it's missing a $ at the beginning. Commented Apr 19, 2016 at 20:05
  • the shorthand document-ready function liberates the $ from wordpress, without it you jave to use the jQuery prefix. Commented Apr 19, 2016 at 20:11

1 Answer 1

2

Your form is functioning properly. What you are missing is to specify in your js for the form to not execute via the default functionality.

Your code should look like this:

(function($) {
   $(document).ready(function() {
      $('#hfwpform').submit(function(e) {
         e.preventDefault(); //this is key
         $response = "This is it!!!";
         alert("Submitted");
         $("#feedback").text($response);
      });
   });
 })( jQuery );
Sign up to request clarification or add additional context in comments.

3 Comments

Your the man. That solved the problem with the reload. I actually had another problem that the path the the js folder couldn't be found. Wordpress was using the Theme path as starting point instead of the plugin path. So I had to change the path to somthng like this: ../../../plugins/my-plugin/js/hf_test.js
Do you by chance know how to fix this in a better way.
Awesome! Glad you got it. You could use $_SERVER["DOCUMENT_ROOT"] . "/and so on". Or maybe checkout codex.wordpress.org/Function_Reference/get_home_path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.