I am trying to create a function(see itemClicked() function below) that takes as parameter(the id of the 'ShopFrame' element's child)- a selector so I don't have to repeat code all over again.
The result is the .click() function firing up automatically, and it looks like the parameter i pass in to the function is recognized or something else is wrong even though there are not any errors in the console. Also I can see the 'p' value I passed the .itemClicked() function in the console log shown without calling the function. Seeing that I supposed it is firing outside the .click() function that was used to call the function itself
What am I doing wrong? Here is my full code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="Menu">
<ul>
<li id="WindowShopButon" ><span class="VMenu">Windows</span></li>
<li id="DoorShopButon" title="#DoorShopCode"> <span class="VMenu">Doors</span></li>
<li id ="BlindsShopButon"><span class="VMenu">Blinds</span></li>
<li id="OptionsShopButon"> <span class="VMenu">Options</span></li>
</ul>
</div>
<div id="ShopFrame">
<p class="shop activeShop" id="MainShop">I am MainShop</p>
<p class="shop" id="WindowsShopCode" > I am WindowsShopCode</p>
<p class="shop" id="DoorShopCode">I am DoorShopCode</p>
<p class="shop" id="BlindsShopCode">I am BlindsShopCode</p>
<p class="shop" id="OptionsShopCode">I am OptionsShopCode</p>
</div>
<script type="text/javascript">
function hoverItem() {
$(this).toggleClass("hover")
};
var p = '';
function itemClicked(p){
$('.VMenu').removeClass("active");
$(this).addClass("active");
$("#MainShop").remove();
$('.shop').removeClass("activeShop");
$(p).addClass("activeShop");
console.log(p);
}
$( document ).ready(function() {
console.log( "ready!" );
$("#WindowShopButon span.VMenu")
.click(function() {
$('.VMenu').removeClass("active");
$(this).addClass("active");
$("#MainShop").remove();
$('.shop').removeClass("activeShop");
$("#WindowsShopCode").addClass("activeShop");
})
.hover(hoverItem);
$("#DoorShopButon span.VMenu").hover(hoverItem).click(itemClicked('#DoorShopCod'));
$("#BlindsShopButon span.VMenu").click(function() {
$('.VMenu').removeClass("active");
$(this).addClass("active");
$("#MainShop").remove();
$('.shop').removeClass("activeShop");
$("#BlindsShopCode").addClass("activeShop");
})
.hover(hoverItem);
$("#OptionsShopButon span.VMenu").click(function() {
$('.VMenu').removeClass("active");
$(this).addClass("active");
$("#MainShop").remove();
$('.shop').removeClass("activeShop");
$("#OptionsShopCode").addClass("activeShop");
})
.hover(hoverItem);
});
</script>
<style>
.activeShop{display: block
!important;}
.inactiveShop{display: none!important;}
.active, .hover{
border-bottom: #6ba1b2 1.5px solid;
padding-bottom: 5px;
cursor: pointer;
}
</style>
Thanks!