it removes the class but it doesn't add the class, i can't use .click() function it needs to be onclick=""
<li class="selected">Click me</li>
<li>Click me</li>
function myfunction(a,b,c){
$('ul li.selected').removeClass('selected').addClass('none');
$(this).closest('li').addClass('selected');
}
You are already using jQuery, so just use jQuery to handle all your events.
Add this code;
$(function(){
// bind click event to your <a> tags
$('a').click(function(e){
// prevent the default behaviour of your links
e.preventDefault();
// remove the class from all li's
$('li').removeClass('selected');
// add selected to the clicked a's parent li
$(this).parent('li').addClass('selected');
});
});
Then keep your html clean from JavaScript
<li class="selected">
Click me
</li>
<li>
Click me
</li>
p.s.
If you are wanting to put PHP in, then you could put things in like;
// check out my 1337 php coding skillz!
Click Me
Then access it in your jQuery using
$(this).data('foobar');
pps
Sorry my php coding skills are rubbish!
Related
This question already has an answer here:
Add ".active" class to the current page's link in a menu using jQuery or PHP [closed]
(1 answer)
Closed 8 years ago.
I cant find a way to add a class to an "a" element of a nav bar.
This is the html nav and the jQuery (applied to test.html url only, to test it):
$(document).ready(function() {
$("a").click(function() {
var actRef = $(this).attr("href");
if (actRef === "test.html") {
$("a[href='test.html']").addClass("active");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<nav>
<ul>
<li>Inicio
</li>
<li>Test
</li>
<li>Item
</li>
<li> test2
</li>
</ul>
</nav>
This isnt working, it just doesnt do anything, but if i change the .click to .hover it works fine adding the class but doesnt go to the link so the info of the page wont change.
I also tried this: How to change active class while click to another link in bootstrap use jquery? but neither works because of the e.preventDefault();...
If i take out the preventDefault it wont add the class but it will forward to the link...
PD: The aim is to let the user know on which tab he is actually on depending on the menu link he clicked.
Why not use then anchors :active state:
a:active {
/* your .active styles */
}
You code is not working as you are trying to set class on some link using javascript, and then navigating same time. So thing is the part where you are changing class for link is working actually, however you are not able to see it as after navigation html will be reloaded.
To solve this problem , you need write a common function for updating the class of link, in your common html. However, call that function from the html being loaded at onload event instead of calling at click.
Your common js or html will be having function :-
highlightlink(linkid){
$("a[href=' + linkid +']").addClass("active");
}
Each html will call this functin onload with respective htmlname.
For example test.html will hat this code :-
$(document).ready( function (){
highlightlink('test.html')
});
});
While index.html will have :-
$(document).ready( function (){
highlightlink('index.html')
});
});
Once your html is loaded, the that particular html will loaded
Do you really need to add a class to the html element? If it's about styling I think it might be possible to solve your styling using a simple :active pseudo selector. See example below:
li:active {
background-color: #f99;
}
li a:active {
color: #fff;
}
<nav>
<ul>
<li>Inicio
</li>
<li>Test
</li>
<li>Item
</li>
<li> test2
</li>
</ul>
</nav>
So what you can do is you can add a page link in the URL querystring like:
www.example.com/test.html?pageinfo=test.html
Now after the successful page loads you can retrieve page info from the query string and then you can add an active class to the anchor tag.
This way you will get querystring like this pageinfo=test.html and after successful parsing of querystring you will convert the information to {pageinfo:test.html}.
Using that you can add style/class to the anchor tag.
Thanks to #Panther this is easier than i tought:
$(document).ready( function(){
var location = window.location.pathname;
location = location.replace("/", "");
$("a[href='"+location+"']").addClass("active");
});
I have multiple pages that all use a simple tab menu at the top.
The menu (aka list) is stored in a separate html file:
<ul>
<li id="Tab1"><a href='../Page1.html'> Page1 </a></li>
<li id="Tab2"><a href='../Page2.html'> Page2 </a></li>
<li id="Tab3"><a href='../Page3.html'> Page3 </a></li>
every page has <div id='tabs'> </div> and
$('#tabs').load('CommonTabs.html');
Everything works fine, my tabs load into the 'tabs' div. Now I want to set a class of one div on each page to "active", so that the proper style defined in a css can be applied.
I tried:
$('#tabs').load('CommonTabs.html');
$('#Tab1'.addClass("active");
But that doesn't seem to work. When I do "inspect element" in a browser, the li does not have the "active" class assigned.
The load function is asynchronous, so the data is not loaded when the second call executes. Perform it in a callback function instead:
$('#tabs').load('CommonTabs.html', function () {
$('#Tab1').addClass("active");
});
You have a syntax error. Try:
$('#Tab1').addClass("active");
I will give you an example to set a class to a specific element on a button click. Of course you will need to create a button with the class addClassButtonTab3 for this to work.
$('.addClassButtonTab3').click('on', function() {
$('#tab3').addClass('active');
});
We are binding an event handler to a button with class 'addClassButtonTab3' and when it is clicked we are adding a class to an element with ID tab3.
I am having several links in asp pages and all links are having respected CSS. the 1st links is highlighted on the Home page with Different CSS. I want to toggle the CSS class on the the Click event whenever i pressed the 2nd or the the 3rd link respectively it should get highlighted and other one become Normal with Normal CSS.
<ul>
<li><a href="../Admin/Home.aspx" id="a_Home" class="homeactive" onclick="ChangeSelectedMenuCss(this.id);">
Home</a></li>
<li><a href="../Admin/subadmindetails.aspx" id="a_Report" class="home" onclick="ChangeSelectedMenuCss(this.id);">
SubAdmin</a></li>
<li><a href="../Admin/control_panel.aspx" id="a_User" class="home" onclick="ChangeSelectedMenuCss(this.id);">
Control Panel</a></li>
<li><a href="../Admin/admin_master.aspx" id="a_CntrlPnl" class="home" onclick="ChangeSelectedMenuCss(this.id);">
Master Data</a></li>
<li>Logout</li>
</ul>
please help me out i m stucked
Thanx and regards.
I think you're confusing how ASP.NET and Javascript interact with each other. When a user clicks on one of those links, the onclick event will fire, but then ASP.NET will load the page that relates to the link, therefore resetting the navigation menu.
What you probably want to do instead of using onclick events is to have a class on your Masterpage that identifies what page it is on, and then add the homeactive class to whatever link it needs to be on.
In order to change class using javascript you can do something like this:
function ChangeSelectedMenuCss(id){
document.getElementByClassName('homeactive').className ="home";
document.getElementById(id).className = "homeactive";
}
If you use JQuery, then this code may be useful for you.
First of all like this code-
$(function() {
var links = $('a.link').click(function() {
links.removeClass('active');
$(this).addClass('active');
});
});
And then in your CSS File, Add tis class-
a, a:visited { color:black }
a.link.active { color:blue; }
It might Help you....
or you can see this fiddle - http://jsfiddle.net/gHb9F/
I have the following code:
<div id="nav">
<ul>
<li id="tabOne" class="first current">Page One</li>
<li id="tabTwo">Page Two</li>
<li id="tabThree"><a href="./CS3.html" target="SheetView">Page Three</li>
<li id="tabFour">Page Four</li>
<li id="tabFive">Page Five</li>
<li id="tabSix">Page Six</li>
</ul>
This loads the selected page into an iframe named "SheetView." What I need to do is use JavaScript to alter the class when an option that isn't the currently selected on is clicked. I should say that I have the current class already setup in my CSS. I just have no way to trigger it.
I thought adding an onlick event to the <UL> up there and calling onclick="Javascript:changeCurrent();" but there is the problem (four actually):
Is <ul onclick="JavaScript:changeCurrent();> where I need to have the event?
What is the resulting JavaScript to make the change happen?
How can I cause the first option to be set as current by default?
Is there a way to keep the currently selected option from being an active link?
I found a few different examples but I couldn't tailor them to work for me. Any help would be most appreciated.
Since you specified that you wanted a non-jQuery response, here's a function that will toggle appropriately:
function toggleNavSelected(el){
var list = document.getElementById('nav').children[0];
for(var i=0; i<list.children.length; i++){
var cur = list.children[i];
if(el==cur){
cur.classList.add("current");
cur.firstChild.onclick = (function(){
toggleNavSelected(this.parentElement);
return false;
});
} else {
if(cur.classList.contains("current")){
cur.classList.remove("current");
}
cur.firstChild.onclick = (function(){
toggleNavSelected(this.parentElement);
});
}
}
}
Either add an onclick handler to each LI (onclick="toggleNavSelected(this);") or execute the following after the menu has loaded:
var list = document.getElementById('nav').children[0];
for(var i=0; i<list.children.length; i++){
var el = list.children[i];
el.firstChild.onclick = (function(){
toggleNavSelected(this.parentElement);
});
}
Demo: http://jsfiddle.net/bWY7P/2/
(note: The JSFiddle script has a small difference; it adds a return false; to the onclick function so that you can play with it without the links actually following the HREF attribute. Do not use that line in your live code)
Explanation:
The function looks at each LI element within the #nav element.
If that element is the element passed to the function, then it adds the class .current.
Otherwise, it removes the class .current (if present).
The second part binds a function to the onclick event of each a element that calls the toggleNavSelected() function and passes its parent element (the li) as the argument.
1) if you want to change the currently selected class when you click an item, put the onclick into the li item
2) using jquery would be very easy here, all you have to do is import the jquery file with the <script> tag and you're ready! For example, you could do onclick="changeClass(this);" on the <li> tag and in a normal JavaScript file or in a script tag:
function changeClass(this){
$('#nav li').attr("class","");
$(this).attr("class","current");
}
Replace the 'current' with the class name you want to use
3) it should already be set as current
4) use the :visited CSS selector to change what colour followed links look like eg:
a:visited{
color: #000000;
}
First of all you should set the event handler from a separate script, not from an onclick attribute. You don't repeat your code that way and have anything in one place. The HTML is also much cleaner.
Using jQuery it would be as easy as:
var menuLinks = jQuery( '#nav a' );
menuLinks.on( 'click' function() {
menuLinks.removeClass( 'active' );
$( this ).addClass( 'active' );
} );
You could also do that in plain JS, but using some library keeps you out of the trouble of browser incompatibilities.
I have a list with links:
<li class="link-1">One</li>
<li class="link-2">Two</li>
<li class="link-3">Three</li>
..
user clicks on any link, then with jQuery I want to display the content of the link.. somthing like:
$(".link-??? a").click(function() {
alert($(".link-??? a").html());
})
something like this. I am not going to create X function (as the number of the links), so what can I do? I should replace the ??? in somtehing else..
You could do:
$('li[class^="link"] a').click(...
However this would only work if the li have only one class or if the link-X class is the first in the list.
Inside the handler you can use $(this) to refer to the a element:
alert($(this).text());
Much better would be to give the li elements a common class:
<li class="link">One</li>
<li class="link">Two</li>
<li class="link">Three</li>
$('.link a').click(... will be much more reliable.
Give each element the same class. Then in your javascript reference this within your function. Check out the link below to see a working example
http://jsfiddle.net/kprgr/2/
<li class="link">One</li>
<li class="link">Two</li>
<li class="link">Three</li>
$(".link").click(function() {
alert($(this).find("a").html());
});
Try..
$(".link-??? a").click(function() {
alert(this.innerHTML);
})
Inside the click event, this should refer to the element that was clicked.
You could also do..
alert($(this).html());
..but the first way is simpler, and faster.