How to use jquery to get to previous li - javascript

I have the following html:
<li>
Presentation Name
</li>
<li>
Survey Length
</li>
and here is the jquery:
var yolo = $('.not-allowed').first();
yolo.removeClass("not-allowed");
yolo.prev("li").addClass("active");
The first two lines of javascript are working. For some reason, i can't get the last line to work. I'm trying to have the second li tag have the class active, but the class is just not being added anywhere.

There is no previous element for that link. You want to get the parent of the link:
yolo.parent().addClass("active");
Ref: .parent()
jsFiddle example
Note that you could also use .closest('li') instead of .parent().

You want parent() not prev().

.prev() queries only the siblings of the element, and .not-allowed has none. In order to use .prev() you'd need to call .parent() to get the list item element the anchor is within:
yolo.parent().prev("li").addClass("active");
Edit: I'm an idiot

Have a look at this with the example. I think this answers your question: https://api.jquery.com/prev/
Markup
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li class="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
JQuery
$( "li.third-item" ).prev().css( "background-color", "red" );
I made a JSFiddle here: https://jsfiddle.net/ToreanJoel/nq6efza9/

Related

find next elements but not in order having same class

I want to find elements which have .item class but they are not in order. Below is sample code.
<ul>
<li class="item">list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li class="item">list item 4</li>
<li>list item 5</li>
<li class="item">list item 6</li>
</ul>
I want to traverse each li element having only .item class on key-press event. I have tried .nextAll() getting all elements but it takes much time to produce resultsets as I have huge li elements.
var siblingsElements = $('.item').nextAll(); // Takes much time
How can i achieve the result ? Thanks in advance.
Try to rewrite code with Native JS, it works much faster
var items = $('ul')[0].getElementsByClassName('item');
items = $(items);
You can try
$('li.item').each(function(){
--your code here--
})
Because this will find only li having class "item" instead of all dom elements having class "item" which may speed up your process.

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());
});

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.

Find the last child or grandchild in a UL

I have a CMS generating a basic navigation using a UL. Each first-level LI is styled to be a group. I need to apply a style to the last element within each of the first level LI "groups". Meaning; if a first-level LI has no children, I want to apply a style to it (as the "bottom" of the group); if it does have children, I want to find the last child OR grandchild element that appears (again, the "bottom" of the group). I have used both CSS and Javascript "last" classes, and have successfully applied styles to the last child of a certain depth within the first-level LI, but that isn't helpful since the bottom button of the list is of an unknown depth.
My line of thinking involves a IF statement that finds the last child of the first level, checks if it has children, and if it does, go another level deep and find that last child and checks for children, repeating this process until it finds the last LI that does not have children within the first-level LI groups. However, I am a JS noob and am not sure how to go about that.
I am open to CSS or JavaScript/jQuery solutions. I have been banging my head on this one for a while and appreciate any input or better ideas. Thanks for your help!!
--
Update: here's a code sample of what I am hoping for:
<ul id="navigation>
<li>Item one</li> <!--Style this one-->
<li>Item Two
<ul>
<li>Item Two-One</li>
<li>Item Two-Two</li> <!--Style this one-->
</ul>
<li>
<li>Item Three
<ul>
<li>Item Three-One</li>
<li>Item Three-Two
<ul>
<li>Item Three-Two-One</li>
<li>Item Three-Two-One</li> <!--Style this one-->
</ul>
</li>
</ul>
<li>
<li>Item Four
<ul>
<li>Item Four-One</li>
<li>Item Four-Two
<ul>
<li>Item Four-Two-One</li>
<li>Item Four-Two-One</li>
</ul>
</li>
<li>Item Four-Three</li> <!--Style this one-->
</ul>
<li>
</ul>
The reason the .last selector doesn't work is because, in the above example, it would style item Four-Two-One since it is the last element of its UL.
To clean up my answer, incase this is read at a later date.
here is my solution to your problem:
var checkChildren = function($this) {
if (!$this.children().length) {
$this.css({
color: '#f00',
fontWeight: '700',
textDecoration: 'underline'
});
return false;
}
return true;
};
$("#navigation").children("li").each(function() {
$these = $(this);
while (checkChildren($these)) {
$these = $these.children().last() || $these.next();
}
});
I think this is what you want. It's part of the jQuery API.
http://api.jquery.com/last/
HTML
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
jQuery
$('li').last().css('background-color', 'red');
EDIT:
Okay I figured out a way for you to do it.. the only caveat is that you need to be able to set a class for the first level of <li>'s. I've posted my findings here:
http://jsfiddle.net/TTXch/54/
It selects the last of the <li>'s for each main navigation element. Your posted structure was a little off... the end tags didn't all match up. Anyway, check out the jQuery in the javascript window in the jsFiddle.
You should be able to use the jQuery last selector for this (http://api.jquery.com/last-selector/).

Categories