Adding custom JavaScript file in WordPress

I have child theme in which I have a page test.php. In this page, I have a select element. When value of option is RED then heading should be displayed. I wrote JavaScript Code for this, but it is no working. PHD and JavaScript Code is mentioned below.

    <Select id="colorselector">
       <option value="red">Red</option>
       <option value="yellow">Yellow</option>
       <option value="blue">Blue</option>
    </Select>

    <div id="red" class="colors" style="display:none">
        <h1>Red</h1>
    </div>

JavaScript is

       <script>  
          $(document).ready(function(){
          $('#colorselector').on('change', function() {
          if ( this.value == 'red')
          {
              $("#red").show();
           }
          else
          {
          $("#red").hide();
          }
       });
       });
     </script>

I wrote JavaScript code into file custom.js that is located in same folder as of test.php and style.css. I put following code in function.php of child theme.

     add_action('wp_enqueue_scripts', 'tutsplus_enqueue_custom_js');
     function tutsplus_enqueue_custom_js() {
     wp_enqueue_script('custom', 
     get_stylesheet_directory_uri().'custom.js', array(), false, true);
     }

expected output is not met...What did I miss? Can anyone guide me?

I also follow Method 2 in link. I use following code in function.php, But it doesn't work.

    function wpb_hook_javascript() {
    ?>             
    <script>
    $(document).ready(function(){
    $('#colorselector').on('change', function() {
    if ( this.value == 'red')
    {
    $("#red").show();
    }
    else
    {
    $("#red").hide();
    }
    });
    });
    </script>

    <?php
    }
    add_action('wp_head', 'wpb_hook_javascript');

Upvotes: 0

Views: 585

Answers (3)

Pirate of KGP
Pirate of KGP

Reputation: 66

Wordpress converts all '$' to 'jQuery' so that it doesn't conflict with any php $(if any mistake happen). We need to convert it again to '$' object. And also need some changes on your code.

<script>  
 jQuery(document).ready(function($){
  $('#colorselector').on('change', function() {
    if ( $(this).val() == 'red') {
        $("#red").show();
    } else {
        $("#red").hide();
    }
 });
});

Upvotes: 0

SOS9GS
SOS9GS

Reputation: 347

Follow this code:

add following code to your functions.php file

add_action('wp_footer', 'my_code');

function my_code(){
 ?>
<script>  
jQuery(document).ready(function($){
    $('#colorselector').on('change', function() {
        if ( $(this).value == 'red')
        {
            $("#red").show();
        }
        else
        {
            $("#red").hide();
        }
    });
});
</script>
<?php
}

Hop this will solve your issue.

Upvotes: 1

kingkong.js
kingkong.js

Reputation: 753

There is a Wordpress plugin called Simple Custom CSS and Javascript. See https://sv.wordpress.org/plugins/custom-css-js/

Download that -> Create new .js file from admin panel and enter your code there

If you don't want to use plugin, follow this guide: https://www.wpbeginner.com/wp-tutorials/how-to-easily-add-javascript-in-wordpress-pages-or-posts/

Upvotes: 0

Related Questions