Using JQuery on a specific page
-
There are two different radio buttons on a form page. If one of them is selected, I want to show the element (div) and if the other is selected, I want to hide it.
I decided to use Javascript for this. While the code works in the browser, it doesn’t work when I add it.
I don’t understand where is wrong.
If I write the code like this, I get the known error Typeerror: $ Is Not a Function in the console:File Location: https://www.example.com/my-js.js
jQuery(document).ready(function() { if(jQuery('#field-x').is(':checked')){ $('.field-field-a').hide(); } if($('#field-y').is(':checked')){ $('.field-field-a').show(); } });
As a solution to this, I made the following definition:
var $ = jQuery.noConflict(); $(document).ready(function() { if($('#field-x').is(':checked')){ $('.field-field-a').hide(); } if($('#field-y').is(':checked')){ $('.field-field-a').show(); } });
The error has disappeared. However it still doesn’t work. Also, there is no error in the console.
I’ll be grateful if you help.function.php
function load_js_assets() { if( is_page(49) ) { wp_enqueue_script('my-js', 'https://www.example.com/my-js.js', array('jquery'), '', true); } } add_action('wp_enqueue_scripts', 'load_js_assets');
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Using JQuery on a specific page’ is closed to new replies.