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