I have some lines of code that filter wordpress-categories by AJAX after you chose the cartegory in a dropdown selectbox. But you have to press a button for starting the process. I would like to have this without pressing the button.
I thought that changing "submit" into "change" in the first line will do the job. But it does not work. Therefore my question: How can i avoid the button and make it run directly after choosing the category from the dropdown?
The Dropdown with button:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<?php
if( $terms = get_terms( array( 'taxonomy' => 'category',
'orderby' => 'name',
'include' => array(32, 33, 34, 35, 36, 41 ) ) ) ) :
echo '<select name="categoryfilter"><option value="">Categorias</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
?>
<button class="emed_go">GO!</button>
<input type="hidden" name="action" value="myfilter">
</form>
<div id="response"></div>
And here the jQuery:
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(),
type:filter.attr('method'),
beforeSend:function(xhr){
filter.find('button').text('Processing...');
},
success:function(data){
filter.find('button').text('Apply filter');
$('#response').html(data);
}
});
return false;
});
Thanks rabox