0

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');

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

open your browser console and make sure there is no JavaScript's error. Let me know @MuhammadAboBakarAslam
0

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/

2 Comments

But I don't want to use any plugin. Is there any solution without using plugin?
I also follow [link] (wpbeginner.com) guide in Method.2.
0

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();
    }
 });
});

Comments

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.