Avishay28 Avishay28 - 6 months ago 21
Javascript Question

click() doesn't work with option tag

I want to target specific

option
by the option value, but seems that the click function doesn't work on
option
tags.

<select name="page_template" id="page_template">

<option value="default">regular template</option>
<option value="page-templates/100-full-width.php">100% Full Width Page</option>
<option value="templates/form-embed.php" selected="selected">Form Embed</option>
<option value="page-templates/full-width.php">Full Width Page</option>
<option value="templates/homepage.php">Homepage</option>

</select>


JS:

jQuery(document).ready(function($){

$('option[value="templates/form-embed.php"]').click(function(){
//alert('test');
$('.wp-editor-expand').toggleClass('hide');
});

});


I want to check weather the option was clicked, regardless of weather it selected or not, as selected is set just after submitting the form.

Answer

Option tag doesn't have click event handler, use select's change event instead

$('#page_template').change(function(){
    if ( $(this).val() == "templates/form-embed.php" )
    {
       $('.wp-editor-expand').toggleClass('hide');
    }
});
Comments