using jQuery $(this).addClass not working, simple code not working - javascript

this simple code is not working, just trying to add a class once clicked on.
$('a').click(function(){
//alert('on'); WORKING
$(this).addClass('on');
})
The HTML is simply..
<ul>
<li><a href="">List Item 1</li>
<li><a href="">List Item 2</li>
<li><a href="">List Item 3</li>
<li><a href="">List Item 4</li>
</ul>

You didn't ever close your <a>, but it's working for me otherwise.
http://jsfiddle.net/L3nyE/
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
</ul>
CSS:
.on { background-color:red; }
jQuery:
$('a').click(function(){
//alert('on'); WORKING
$(this).addClass('on');
});

Prevent the default behaviour
$('a').click(function(event){
event.preventDefault();
$(this).addClass('on');
});

Works for me, provided you either change the target of the links to "#". (Or return false from the click handler.)
http://jsfiddle.net/jaNCq/1/
You never close your <a>'s but Firefox is smart enough to close them for you. Can't say for other browsers though.

Your code working fine, check so you load your code when the dom is loaded and ready.
Working example
$(document).ready(function(){
$('a').click(function(){
$(this).addClass('on');
});
});

Don't forget to close your "a" tags. Also add a ";" at the end of your jQuery function.

Make sure you have the jquery lib referenced and wrap your code in the dom ready. This worked for me.
<style>
.on{
color:red;
}
</style>
<script>
$(function () {
$('a').click(function () {
//alert('on'); WORKING
$(this).addClass('on');
})
});
</script>

Your HTML is Incorrect
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
<li>List Item 4</li>
Demo

Related

jquery navigation always opens the first option

I have a nav element which is something like this:
<ul>
<li name='first_item'>
<ul>
<li>item 1</li>
<ul>
<li>item 1.1</li>
<li>item 1.2</li>
</ul>
<li>item 2</li>
</ul>
</li>
</ul>
<ul>
<li>
<ul>
<li>item 3</li>
<li>item 4</li>
</ul>
</li>
</ul>
and the code that handles the the sliding down and up is:(nav is a html element which is a parent of above)
nav.find("li").each(
if ($(this).find("ul").length > 0) {
_callback = false;
$("<span>").text("^").appendTo($(this).children(":first"));
//show subnav on hover
$(this).mouseenter(function() {
$(this).find("ul").stop(true, true).slideDown();
});
//hide submenus on exit
$(this).mouseleave(function() {
$(this).find("ul").stop(true, true).slideUp();
});
}
});
what happens is when I hover over the first_item it opens the sub menus and after it's finished sliding down them, it will open item 1's sub menus as well. I'm totally lost over this. Any help would be appreciated.
First of all, it seems you copyied the jquery without the function, so that isnt the problem:
nav.find("li").each(function(){
I think the problem is, that you travel to deep, so try this:
$(this).find(">ul")
or this:
$(this).children("ul")
From jQuery:
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.

Parallel element selector

We have the following situation:
<ul>
<li>Button 1</li>
<li class="active">Button 2</li>
<li>Button 3</li>
<li>Button 4</li>
</ul>
now the following code that handles it:
$('.tab-menu li').on('click', function() {
$($($(this).closest('.tab-menu')).find('li')).removeClass('active');
$(this).addClass('active');
});
it works, but is there a jQuery function that selects all 'parallel' elements ? I want this
part
$($($(this).closest('.tab-menu')).find('li')).removeClass('active');
make shorter. I know 'toggleClass', but this works only on the clicked element. Is there any shorter method to select all parallel elements? (In this siutation all 'li' that belongs to the specific 'ul');
Try this, using .siblings()
$(this).addClass('active').siblings().removeClass('active');
DEMO with onclick

jquery select li element in hierarchical menu

I need to be able to get the text from the certain li when it is clicked on, code is below:
<div>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>
<ul>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
</li>
<li>list item 6</li>
<li>
<ul>
<li>list item 7</li>
<li>list item 8</li>
<li>list item 9t</li>
</ul>
</li>
</ul>
</div>
In this case, I need to get the "list item 5" by its index.
Edited:
Basically, when the <li>list item 5</li> is clicked i want to get this particallar text or value, when something else is clicked - do nothing. Note that although the menu is hierarchical, 5th element means flat 5th element. Carefully check the texts of the li items to understand.
$('li').on('click',function(){
alert($(this).text());
});
Working Example
Well, it appears that eq selector is sufficient for your purpose.
$("li:eq(5)").click(function(){
var text = $(this).text();
})
Your best solution is to give that item an id or, at least, a class then you can do:
$("#myID").click(function() {
var text=$(this).text();
});
If you can't do this, then you can try using the various navigation selectors, maybe something like (untested):
$("div>ul>li:nth-child(3)>ul>li:nth-child(3)").click(...
But this is a really awkward way to do it and may break the second you add any more items to your list(s). (here's a fiddle - it'll alert only when your list item 5 is clicked)
Or if the text is static, you could select by that:
$("li:contains('list item 5')").click(...
Which, again, will break if that text changes, or if there are other nodes that contain the same text.
$('ul').on('click', 'li', function() {
console.log($(this).text());
});

jQuery .click() .show() function issues

Could anyone please let me know what i'm doing wrong.
I have:
//My HTML
<div>
<ul>
<li id="test">Main Item 1</li>
<ul class="list-in-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<li>Main Item 2</li>
<li>Main Item 3</li>
</ul>
</div>
//My CSS
.list-in-list {
display:none;
}
//My jQuery
$(document).ready(function() {
$('#test').click(function() {
alert("hello");
});
});
My final goal is to show that none displayed content if you press a list item, so that it expands neatly. However, i can't seem to get that alert() appearing in any way. Should i use an id for all list items in the main list, or is it enough with a class?
/W
you can add .next function to show next ul for any li curreny click by user. you have to change id test to one class name to make effect in all click of main li
HTML would be like
<div>
<ul>
<li class="main">Main Item 1</li>
<ul class="list-in-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<li class="main" >Main Item 2</li>
<ul class="list-in-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<li class="main">Main Item 3</li>
<ul class="list-in-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</ul>
</div>
and JQuery function is below
$(document).ready(function() {
$('.main').click(function() {
$(this).next(".list-in-list").slideToggle();
});
});
for detail you can check link
Should i use an id for all list items in the main list, or is it enough with a class?
IDs are unique. Your JavaScript code will not work properly if you have multiple identical IDs. If you're planning on adding a similar attribute to all of your list items you'd use a class in this case (and reference it with . instead of #). In this case you'd call the click function using:
$('li.myClass').click(...);
If you only have one list, however, you can simply add the ID to the ul and use the click function as:
$('ul#myId > li').click(...);
Note that it would be marginally quicker with the classes in this case.
You'd then reference your inner ul using:
$('li.myClass > ul.list-in-list');
Or, depending on which of the above you went with:
$('ul#myId > li > ul.list-in-list');
(You'd use > here to select only the direct child. If you used ul#myId li you'd also be selecting the li elements which belong to any inner ul)
Your code works fine I do believe you have not included Jquery on your page - or maybe the path to it is not valid. Check your network tab to see if you get an http error retrieving jquery.
You can show the hidden li by doing the following:
$(document).ready(function() {
$('#test').click(function(e) {
$(this).find('.list-in-list').show();
});
});
Your code works fine:
Demo
In this case classes would be better than ids because Id's have to be unique on your page. You can use classes like in the demo below by adding a class to your outer li elements. Just change the binding from #test to whatever class you give your li elements.
$('.clickAbleLi').click(function(e) {
$(this).find('.list-in-list').show();
});
Demo
You close your </li> tag before your "list-in-list". You should close your </li> tag after your inside list ;) Like this :
<div>
<ul>
<li id="test">Main Item 1
<ul class="list-in-list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
<li>Main Item 2</li>
<li>Main Item 3</li>
</ul>
</div>
It sholud work but Try moving the ID attribute to the A instead of LI if you experience problems

change class on LI click

I need to dinamically assign a .selected class to the element where I click and also remove any other previous class asigned to the clicked element so I can change CSS class. Maybe this code:
$(this).click(function(){
$(this).addClass('selected');
});
works but what happend if I click in any other LI? Any help to get this work?
EDIT:
Ok see this code:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
By default none have any classes but I click in Item 2 then the HTML should transform on this one:
<ul>
<li>Item 1</li>
<li class="selected">Item 2</li>
<li>Item 3</li>
</ul>
but once again if I click in Item 3 then the HTML should transform on this one:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li class="selected">Item 3</li>
</ul>
This is what I'm trying to do
I'd suggest:
$(selector).click(function(){
$('.selected').removeClass('selected');
$(this).addClass('selected');
});
With regards to the comment left by moonwave99 (below), if you only want to remove the selected class-name from those elements contained within the same parent element:
$(selector).click(function(){
var that = $(this);
that.parent().find('.selected').removeClass('selected');
that.addClass('selected');
});
Though it's worth remembering what element you're clicking, and what the parent will be, for example, clicking on the a in the following HTML:
<ul>
<li>link text</li>
</ul>
Will look within the li for the other .selected elements, and so you should use:
$(selector).click(function(){
var that = $(this);
that.closest('ul').find('.selected').removeClass('selected');
that.addClass('selected');
});
First remove selected class from all li inside ul and then apply class to clicked li.
$('ul li').click(function(){
$('ul li').removeClass('selected');
$(this).addClass('selected');
});
On my travels I discovered that it is not possible to addClass of 'active' to list items in when using bootstrap. So if you are reading this (as I was) wondering why this doesn't work when the classname is 'active' you need to choose something else.

Categories