Alright this is my first day with JQuery so have fun with these functions I made. I’m trying to hide a div (#panel) when different triggers are clicked. Below is three functions in which all three of them need to hide (#panel) when used. The way I have it setup it’s only working for the first function. The other two don’t hide the element. So without further ado.
jQuery(document).ready(function($){
var $panel = $(this).closest(".panel-container").find(".panel");
$('#searchsubmit').click(function(e){
$('#boxes').empty();
e.preventDefault();
var $panel = $(this).closest(".panel-container").find(".panel");
var search_val=$("#s").val();
$.post(
WPaAjax.ajaxurl,
{
action : 'loop_search',
search_val : search_val
},
function( response ) {
$('#boxes').append( response ).masonry( 'reload' );
$panel.hide("slow");
$('.trigger').removeClass("active");
$('.trigger-loop').removeClass("active");
}
);
});
$('#fame.trigger-loop').click(function(){
var $panel = $(this).closest(".panel-container").find(".panel");
$('.trigger').removeClass('active');
$('.trigger-loop').removeClass('active');
$('#fame.trigger-loop').addClass('active');
$('#boxes').empty();
$.post(
WPaAjax.ajaxurl,
{
action : 'loop_fame'
},
function( response ) {
$('#boxes').append( response ).masonry( 'reload' );
$panel.hide("slow");
}
);
});
$('#new.trigger-loop').click(function(){
var $panel = $(this).closest(".panel-container").find(".panel");
$panel.hide("slow");
$('.trigger').removeClass('active');
$('.trigger-loop').removeClass('active');
$('#new.trigger-loop').addClass('active');
$('#boxes').empty();
$.post(
WPaAjax.ajaxurl,
{
action : 'loop_new',
},
function( response ) {
$('#boxes').append( response ).masonry( 'reload' );
$panel.hide("slow");
}
);
});
});
//–html
<li>
<a id="fame" class="trigger-loop active" href="#"><div id="fame-icon"></div></a>
</li>
<li>
<a id="new" class="trigger-loop" href="#"><div id="gold-artist"></div></a>
</li>
<li>
<div class="panel-container">
<div class="panel">
<----content----->
</div>
<a class="trigger" href="#"><div id="playlist-icon"></div></a>
</div>
</li>
<li>
<div class="panel-container">
<div class="panel">
<----content----->
</div>
<a class="trigger" href="#"><div id="random-icon"></div></a>
</div>
</li>
The problem is that you have a global variable which is being overwritten and causing undesired affects.
That line will set
$panelas a global variable since it has no parent scope aside fromdocument.ready. When you are inside one of the callback closures, there is a conflict of interest when$panelis defined again. Consider changing these names to remove the conflict.The result is calling the global
$panelwhen inside ofinstead of calling the localized version you were thinking of. That is why only one of them is hiding.