(issue is solved, but still a question in my last post)
Hi,
I create a navbar… with a menu which is loaded with templates and helpers.
I start to add some styles and hide/show behavior.
My menu is in a hidden block and I show after a click on a button…
In my Template.navbar.onRendered
I added a click behavior to open the menu…
No problem with this button.
My menu has a UL/LI DOM.
I hide all sub UL by css and show them after a click on the parent LI . (simple expand collapse)
All my LI has a link. I stop the propagation ( to avoid close the menu) and preventDefault to avoid to open the link if the link has children…
If it’s the last link of the tree without children, so I open and close my menu.
I added in Template.navbar.onRendered a function…
$('.mainMenu [data-level] a').click(function (e) {
var target = e.target
if ((target.tagName === "A") && ($(target).siblings('[data-level]').length)) {
e.stopPropagation();
e.preventDefault();
$(target).siblings('[data-level]').slideToggle('fast');
} else {
$('.mainMenu:visible').slideUp('fast');
}
});
From my browser, I load the page… sometime it work perfectly, sometime not.
If I refresh, sometime it work again sometime not…
My click event is no more detected on my link. No idea why.
If I execute my function from the console, it work again…
I tested to put the function inside a $(document).ready, on with a “.on(‘click’)”… but same result.
here the onRendered code
Template.navbar.onRendered(function(){
// OPEN/CLOSE the main menu
$('#mainMenuBtn').click(function(e){
e.stopPropagation();
$('.mainMenu').slideToggle('fast')
});
// Close the menu if click outside
$(document).click(function(e){
if ($(e.target).parents('.mainMenu').length === 0) {
$('.mainMenu:visible').slideUp('fast');
}
})
// The menu is a tree ul>li>a with UL children.
// If click on a link which has children, do not open the link and open the next child
// If click on a link which has no children, so do native behavior, open the link and close the menu.
$('.mainMenu [data-level] a').on("click", function(e){
var target = e.target
if ((target.tagName === "A") && ($(target).siblings('[data-level]').length)) {
e.stopPropagation();
e.preventDefault();
$(target).siblings('[data-level]').slideToggle('fast');
} else {
$('.mainMenu:visible').slideUp('fast');
}
})
})
I’m almost sure it’s about the loaded order… But I don’t understand as the code is inside a onRendered and the button to hide/show the menu is working in all case…
an idea ?
Thank’s for reading
Mickael