jQuery event not triggered - ajax

I've this piece of markup. This piece has been loaded through ajax and appended inside a div.
Content of file user-bar.php:
<div id="u-bar">
<ul id="u-nav" class="nav">
<li id="notifications-list" class="dropdown" data-time="" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-comment"></i>
Notifications
<b class="caret"></b>
</a>
<ul class="dropdown-menu" >
<li>Notification One</li>
<li>Notification two</li>
<li class="divider"></li>
<li>Show All Notifications</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-user"></i>
Profile
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>View Profile</li>
<li>Settings</li>
</ul>
</li>
<li>Logout</li>
<div class="clear"></div>
</ul>
</div>
I've scripts.js file as below which is included in the index.php file as <script type="text/javascript" src="js/scripts.js"></script>
$(function(){
loadUserBar();
$('#notifications-list').live('click', function(){
console.log('Test');
$('#notifications-list .icon-comment').removeClass('new');
});
});
function loadUserBar(){
$('#user-area').load('php/user-bar.php', function(){
initBootstrapElems(); //Initializing All popover elements
});
}
index.php file has the div#user-area where the ajax returned markup is inserted.
After the whole page has been loaded, when I click on the list-item #notifications-list, nothing happens. But when I typed in $('#notifications-list').click() directly in the console, the log does appears and the removeClass does occur.
What could be wrong here?

You're right, there's a conflict with dropdown bootstrap (simulated at http://jsbin.com/ijiqec/2/edit).
For some reason (don't ask me why) changing the use of live by on fixed the issue.
Use this piece of code:
$('#notifications-list').on('click',...

Try to put the handler for click on the a tag event.
$('#notifications-list a').live('click', function(){ ... })
Update:
You can't use the same id for all li tags.
You need to change the li.#notifications-list to a class if you have that on each tag and then update the js:
$('.notifications-list a').live('click', function(){ ... })

Related

Bootstrap multi-column dropdown does not change li class active

I have some strange behaviour for the dropdown that I decided to transfer to multicolumn.
<!-- language-all: lang-html -->
<li class='dropdown'>
<a href='#ExtRep' class='dropdown-toggle' data-toggle='dropdown'>Extended reports <b class='caret'></b></a>
<ul class='dropdown-menu multi-column columns-3'>
<div class='row'>
<div class='col-sm-3'>
<ul class='multi-column-dropdown'>
<li class='disabled' disabled>Server 1</li>
<li class='divider'></li>
<li><a href='#tabl1' data-toggle='tab'>Report 1</a></li>
<li><a href='#tabl2' data-toggle='tab'>Report 2</a></li>
</ul>
</div>
<div class='col-sm-3'>
<ul class='multi-column-dropdown'>
<li class='disabled' disabled>Server 2</li>
<li class='divider'></li>
<li><a href='#tabl3' data-toggle='tab'>Report 3</a></li>
</ul>
</div>
<div class='col-sm-3'>
<ul class='multi-column-dropdown'>
<li class='disabled' disabled>Server 3</li>
<li class='divider'></li>
<li><a href='#tabl5' data-toggle='tab'>Report 5</a></li>
</ul>
</div>
</div>
</ul>
</li>
Here is the sample: https://jsfiddle.net/Meeshka/0fazzvdh/9
When selecting each line in dropdown for the first time all is working like a charm. But trying to return to report 3 or 5 I see that I can't do that. While inspecting the code I saw that although I switch to Report 1 or 2, the li element of previous report remains with class="active".
<li class='active'><a href='#tabl3' data-toggle='tab'>Report 3</a></li>
This happens only when I switch between reports in different columns.
Reports in the same drop-down column are working OK.
If I add to each column at least 2 lines, I can manage to switch but not in 100% cases, behavior is unpredictable.
Like in this example: https://jsfiddle.net/Meeshka/0fazzvdh/1/
Is the only way to make it wor correctly is to manually trigger class attribute for the element when I switch to another one?
Is it a bug at all or something I miss in the code?
So far I guess there is no "correct" solution to the bug.
If somebody wants a ready and a very straight forwarded solution, here is how I solved it:
<script type='text/javascript'>
$('a[data-toggle="tab"]').on('click', function(event) {
$(this).closest('div.row').find('li').each(function(){
//console.log($(this).attr('class'));
$(this).removeClass('active');
});
});
</script>
Here's how it looks like in fiddle:
https://jsfiddle.net/Meeshka/0fazzvdh/10/

How to get the xpath for menu in sidebar

I have to click on the Manage Orders option in the side bar menu. I tried with the xpath from firepath, but it is not working. Below is the code:
<div id="sidebar" class="navbar-collapse collapse">
<ul class="nav nav-list" style="height: auto;">
<!-- BEGIN Search Form -->
<li>
<!-- END Search Form -->
<li class="active" data-slug="dashboard">
<li data-slug="order">
<a class="dropdown-toggle" href="#">
<i class="fa fa-shopping-cart"/>
<span>Manage Orders</span>
<b class="arrow fa fa-angle-down"/>
</a>
<ul class="submenu" style="display: block;">
<li data-slug="promocode">
<a class="dropdown-toggle" href="https://bq.training/cp/promocode">
<span>Promo Code</span>
</a>
</li>
<li data-slug="transaction">
<a class="dropdown-toggle" href="https://bq.training/cp/order/list-order">
<span>Order Summary</span>
</a>
</li>
</ul>
</li>
Try Xpath =
//*[contains(text(), 'Manage Orders')]
Use a function like this, is written is c#, just modify slightly.
public IWebElement WaitElement(IWebDriver driver, String element)
{
IWebElement WebElement;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); //can be changed
return WebElement = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(element)));
}
call it like
WaitElement(driver, elementXpath).click();

Drupal dropdown menu that moves content down

I need to have a menu/mega menu that will shift the content down when the dropdown opens like on Facebook for Business menu: https://www.facebook.com/business
I saw the same effect on Avast: http://www.avast.com
Thanks for the help, it's much appreciated!
For behavior you want you can use: slideDown() function of JQuery. Then you can bind mouseover and mouseout events over menu items and call slideDown and slideUp function.
Example in JSFiddle
HTML:
<div class="container">
<div class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href="#">Bootstrap 3</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown active menu-item" data-menuItem="#getstarted">
Getting started <b class="caret"></b>
</li>
<li class="menu-item" data-menuItem="#css">CSS</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="active">Customize</li>
</ul>
</div>
</div>
<div class="col-xs-12 menu-panel" hidden="" id="getstarted">
<ul>
<li>Sign-in page</li>
<li>Sticky footer</li>
<li>Offcanvas</li>
<li>Carousel</li>
<li>Theme</li>
</ul>
</div>
<div class="col-xs-12 menu-panel" hidden="" id="css">
<ul>
<li>Sign-in page</li>
<li>Sticky footer</li>
</ul>
</div>
</div>
JavaScript:
$(".menu-item").mouseenter(
function(e){
e.preventDefault();
e.stopPropagation();
var $active=$(".active-menu-panel");
$active.removeClass("active-menu-panel").hide();
var selector=$(this).attr("data-menuItem");
if($active.length){
$(selector).addClass("active-menu-panel").show();
} else {
$(selector).addClass("active-menu-panel").slideDown();
}
}
);
$(".menu-panel").mouseleave(function(){
$(this).removeClass("active-menu-panel").slideUp();
});

Bootstrap hyperlink in navbar dropdown

Hyperlink in navbar dropdown doesn't seem to work.Even the "disabled" doesn't seem to work.
Here is my code
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle disabled"
data-toggle="dropdown" href="http://www.google.com"> Link
<b class="caret"></b> </a>
<ul class="dropdown-menu">
<li>menu1</li>
<li>menu2</li></ul>
</li>
</ul>
</div>
</div>
</div>
Clicking the link where you have set Google as the link won't take you to that link as the click action on that link opens/closes the dropdown menu.
If you put a link on another anchor tag you'll see that those will work fine.

Twitter Bootstrap Dropdown Hover Open and Stay Active

I read the question on how to get the dropdown to open on hover which was an excellent fix, however I would like to get the nav li to keep "open" class when going down into "dropdown-menu" ul. Here is a jsfiddle http://jsfiddle.net/someyoungideas/29txm/ for my problem.
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Help</a>
<ul class="dropdown-menu">
<li>New Item</li>
</ul>
</li>
</ul>
</div>
</div>​
One way to address your concern is to make use of the twitter-bootstrap-hover-dropdown plugin found on github. The plugin is also one of the answers in the old question: https://stackoverflow.com/a/12851616/538962.
Note the use of data-hover="dropdown" to call the plugin.
<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown"
data-delay="1000" data-close-others="false">Help</a>
Here is a working sample:
http://jsbin.com/xuwokuva/1

Resources