I am having a menu with sub menu.. this menu is coming from Data Base. on clink one of main menu a sub menu opens.. I want that when I click on any link of sub menu.. the sub menu remains open and the selected sub menu should remain selected. but problem is that when I click on any sub menu link.. I reached it's relevant link.. but sub menu get hide . I tried to using J-query to do so.. but failed due to loading new page... please tell me any way to do so.. now i want to use jquery or ajax solution of this
After reading your problem i think this may help you.
Use jQuery(include jQuery Library).
On page you want to keep selected Main menu and submenu, Do the follwoing:
Get Main menu and sub-menu ID and toggle it with jQuery.
$("#mainmenuid").toggle('fast');
$("#submenu").toggle('fast');
OR you can apply any jQuery events.
Note: Add jQuery in the header area of that particular page page.
You can try this :
$('#menu > li > a').click(function(){
if($(this).siblings('ul').length > 0){
$(this).siblings('ul').show();
return false; // This will do what you want.
}
});
This will not allow links if has any sibling "ul" elements. Otherwise, it will be go to it's href.
Test here :
http://jsfiddle.net/SXKG4/2/
You can try load if you want an ajax solution:
suppose this is the html:
<ul id='main-menu'>
<li><a href='pages/home.html'>Home</a></li>
<li><a href='pages/about.html'>About us</a>
<ul class='sub-menu'> /*initially hide it display:none;*/
<li><a href='pages/subpage1.html'>sub link 1</a></li>
<li><a href='pages/subpage2.html'>sub link 2</a></li>
</ul>
</li>
</ul>
<div id='content'></div> <!--here you have to put page contents-->
Then you can use this jQuery:
$(function(){
$('#main-menu li').click(function(){
var page = $(this).find('a').attr('href');
$('#content').load(page);
$('.submenu',this).toggle();
});
$('#main-menu a').click(function(e){
e.preventDefault(); // <-----this stops page to navigate to link href
});
$('.submenu li').click(function(ev){
ev.stopPropagation(); //<----stops the event bubbling to the parent
});
});
Related
I have tried to look for this, but I am not sure exactly how to phrase what I am looking for, so please excuse me if this is a duplicate question. I have a .php page with 5 divs that are link controlled by jquery. loading the page initially has the home div displayed and the rest hidden. Clicking on a link causes the home div to fade and the new div to show in it's place using a jquery script called at the bottom of the page. It is also shown in the url as http://example.com/#visible-div. What I would like to be able to do is link to the page and have a specific section load. For example, if I link to http://example.com/#div4, then the page will load with div4 showing. I was thinking that by using a php get command at the beginning, I could get the request and load it, but I am not sure how I would go about that. Here is my existing code:
<div class="menulistbot">
<ul id="divchange">
<li><a class="homediv" href="#home">Home</a></li>
<li><a class="div1" href="#div1">div1</a></li>
<li> <a class="div2" href="#div2">div2</a></li>
<li><a class="div3" href="#div3">div3</a></li>
<li><a class="div4" href="#div4">div4</a></li>
</ul>
</div>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<script call>
And here is my controlling script
//Main site operation script
function fadeAll() {
$("#homediv").fadeOut(600);
$("#div1").fadeOut(600);
$("#div2").fadeOut(600);
$("#div3").fadeOut(600);
$("#div4").fadeOut(600);
}
$(".div1").click(function() {
fadeAll();
$("#div1").delay(600).fadeIn(1500);
})
$(".homediv").click(function() {
fadeAll();
$("#homediv").delay(600).fadeIn(1500);
})
$(".div2").click(function() {
fadeAll();
$("#div2").delay(600).fadeIn(1500);
})
$(".div3").click(function() {
fadeAll();
$("#div3").delay(600).fadeIn(1500);
})
$(".div4").click(function() {
fadeAll();
$("#div4").delay(600).fadeIn(1500);
})
As it is the code works fine, but like I said, I want to be able to link directly to div4. Is that possible and if so, how?
I want to do is a Navbar is stored in a file called index.php. This is the main page which shows up. When the user clicks a tab in the Navbar, in the body part, another php shows up and the tab becomes active. When the user clicks an other tab, the "active" should Switch Places. I'm having trouble with showing the php and switching the active class. Any Help?
What I was able to do:
There's a script above the navbar code which describes the path of the .php file and tells to show it where it is bounded with.
The code:
<script type="text/javascript">
function homeshow() {
$.get("index.php");
return false;
}
function aboutshow() {
$.get("about.php");
return false;
}
</script>
One of Navbar's link or Navigation Button should enable the Javascript/jQuery code to show up between <p></p> tags located in the body. I've tried this:
<nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li class="active"><a href="#" class onclick="homeshow();">Home</li>
<li><a href="#" class onclick="aboutshow();">About</li>
</ul>
If your trying to check the active tab in a standard click event then the active class wont have been added to the most recently selected tab until after the event.
Subscribe to the bootstrap show.bs.tab instead.
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
e.target // newly activated tab
e.relatedTarget // previous active tab
})
See here for more info.
You can use jQuery or javascript code to change the active state from the previous li to the one you clicked on.
Example with jQuery:
$('ul.navbar-nav a').on('click', function () {
$('ul.navbar-nav li').removeClass('active');
$(this).closest('li').addClass('active');
})
This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 9 years ago.
Ok, so lets say that I have navbar eg.
<ul>
<li class="selected">home</li>
<li>about me</li>
<li>contact</li>
<ul>
and I have a content on a page that I want to change depending on which <li> is selected without reloading the whole page. It seems a bit pointless to reload the whole page just to update 2 divs.
eg. when home is selected I want to load home.php included in <div class='content'> + change class of <li> home</li> to selected etc.
should I use AJAX for this? or should I use $_GET -> altering the URL?
I am a beginner -> sorry for basic questions.
Thx for any kind of help!
You can use Ajax.
But if you're total beginner, another solution without Ajax :
• put all your content in a single file
• put IDs on your div, related to the content (div containing "about" content = div#about)
• just toggle the div on click, related to the content
Like this (JS with jQuery) :
$(document).ready(function(){
$('nav a').click(function(){
var dest = $(this).attr('href');
$('div.content').fadeOut(); // Hide all content divs
$(dest).fadeIn(); // Show the requested part
// You can do all of this using addClass / removeClass and use CSS transition (smoother, cleaner);
return false;
});
});
HTML updated:
<ul> <li class="selected">home</li> <li>about me</li> <li>contact</li> <ul>
If you had no idea of what is Ajax, I guess this solution is better for you.
To change part of your page from a new request, use Ajax. You can find a lot about it online.
That said, using ajax for basic navigation of a simple website is bad taste. Just do a normal navigation.
I want to create ajax updated tabs for user profile tab in frontend magento like categories tab in product management. I was use this code:
<ul id="page_tabs" class="tabs">
<li>
<a id="page_tabs_account" class="tab-item-link ajax active" href="http://localhost/magento/customer/account/" name="account">Account Dashboard</a>
</li>
<li>
<a id="page_tabs_account_edit" class="tab-item-link ajax notloaded" href="http://localhost/magento/customer/account/edit/" name="account_edit">Account Information</a>
</li>
</ul>
<script>
var FORM_KEY="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>";
var varienGlobalEvents=false;
page_tabsJsTabs = new varienTabs('page_tabs', 'tabcontainer', 'page_tabs_account',[]);
</script>
Also add js file in page.xml like:
<action method="addJs"><script>mage/adminhtml/tabs.js</script></action>
When we click on tab then ajax is call but return data is whole page data like with header, footer, left sidebar. We want only perticular tabs assigned .phtml file data (right sidebar).
So how we get that. If other method is easy then let me know.
Thanks.
You would need to create a custom controller that handles AJAX layouts.
Note that varienTabs will fetch the page set on your href links. Having http://localhost/magento/customer/account/ on your tab links will make varienTabs just fetch the content at that URL.
Also, you will need special markup where the content will be posted. There should be a div which ID consists of the ID of your tab link ID + '_content'. E.g.:
<div id="page_tabs_account_content" style="display: none;"></div>
<div id="page_tabs_account_edit_content" style="display: none;"></div>
(Edit) you would aso need the 'tabcontainer' div. It is the second parameter passed to the varienTabs function. Just wrap the divs above on it.
Im trying to add a simple class/id on my <li> element which is in my menu, by using jquery.
However, when I click on a menu-option my class/id is added by jquery successfully but my site of course is gonna update because it redirects to the choicen site.
The result I end up with is that my class/id is removed because of this update..
How can I bypass this so my class/id stays on the selected menu <li> element.
My code looks like this:
$(document).ready(function()
{
$('li').click(function()
{
$(this).addClass('selected');
});
});
This is the site: http://insatsplutonen.xedge.nu/
Click yourself thru the menu-options. If you click on ex: Blogg the class 'selected will be added on the <li> element but will disappear because of page-load...
Thing is i want selected menu-option to turn white and others stay gray :)
Kind regards /Haris
It seems like you are loading the page based on the query string. When I select "Blogg", I get http://insatsplutonen.xedge.nu/?Blogg
When you load the page and check your query string and find a menu variable there, set a variable to the menu item value, and use it to set the class for the menu item accordingly. Ex. for Blogg item:
<li <?php echo ($queryStrVal == 'Blogg') ? 'class="selected"' : ''; ?>>
If I understand what you're asking, one way to solve this is to have a page class, for example on the body, that you use to identify which menu option is currently "selected". So something like this:
<body class="home">
<ul>
<li id="homeNav"></li>
<li id="aboutNav"></li>
<li id="contactNav"></li>
</ul>
</body>
then your css looks like this:
.home #homeNav,
.about #aboutNav,
.contact #contactNav {
// whatever styles represent "selected" here
}
This way, the "selected" nav item always appears selected on the page it represents.
To do in jQuery, find the <a> with the href of the window location and give its parent, which is li that class. This is happening once they reach the page they wanted, not when they click the link, so even pasting the URL in the browser add .selected to the correct li
var urlend= window.location.href.replace(window.location.origin + window.location.pathname, '');
$('a[href="' + urlend + '"]').parent().addClass("selected");