select the most nested element with same classes - html

i'm trying to make some special menu but i have a problem with selecting the most nested element (div) . Menu will be dynamic so it can change how much divs will be nested in one div. (parents will be created with new childs) so i need to select the last one (the most nested) without using more classes od Ids.
Here is a code i wrote until now:
<div id="strategy">
<div class="selected">
0
<div class="selected">
some text
<div class="selected"> this is the last div, but it can be anytime changed and more childs of this element can be created</div>
</div>
</div>
<div class="selected">
1
</div>
<div>
2
</div>
</div>
and something of css i tried:
div.selected:only-of-type {background: #F00;}
also tried nth:last-child, only-child.. i think everything but there must be some way how to do it.

if you're open to jQuery...
$(document).ready(function() {
var $target = $('#strategy').children();
while( $target.length ) {
$target = $target.children();
}
var last = $target.end(); // You need .end() to get to the last matched set
var lastHtml = last.html();
$('body').append('<strong>deepest child is: ' + lastHtml + '</strong>');
last.css('color', 'blue');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="strategy">
<div class="selected">
0
<div class="selected">
some text
<div class="selected"> this is the last div, but it can be anytime changed and more childs of this element can be created</div>
</div>
</div>
<div class="selected">
1
</div>
<div>
2
</div>
</div>

Related

how to get span text from inner tags using jQuery

I want to get the span text using jQuery so my html code is like below
Note :- actually this logic was in table data so I given example code only.
<div class="strategiesddata">
<div class="strattabletodivmobright">
<p class="stategybuysellchkbox clearfix">
<span class="BUY active" name="spbuy">B</span>
<span class="SELL " name="spsell">S</span>
</p></div>
<div class="strattabletodivmobright">
<p class="stategybuysellchkbox clearfix">
<span class="BUY " name="spbuy">B</span>
<span class="SELL active" name="spsell">S</span>
</p></div>
</div>
In the above html code I have two active, buy or sell div's so my query is if it active span then I should take that span text
ex:- From first div B and next div S text I should take
For this I have written jQuery like this
$(".strategiesddata").each(function () {
t = $(this);
var tst = t.find(".stategybuysellchkbox").closest(".active").html();
});
And
$(".strategiesddata").each(function () {
t = $(this);
var tst = t.closest(".stategybuysellchkbox").find("span[.active]").html();
});
But I'm getting the value is undefined Suggest me where I did the mistake and how to achieve this?
Your jQuery statement is wrong, try this.
var tst = $(".stategybuysellchkbox").find(".active").text();
To reference the elements in question, you should be able to simply use
$(".stategybuysellchkbox span.active")
One issue here is that jQuery's methods like text() and html() will only get you the contents of the first element in the collection.
To get an array of all the text, use .map()
const activeText = $(".stategybuysellchkbox span.active")
.map((_, span) => span.textContent).get();
console.log(activeText);
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
<div class="strattabletodivmobright">
<p class="stategybuysellchkbox clearfix">
<span class="BUY active" name="spbuy">B</span>
<span class="SELL " name="spsell">S</span>
</p></div>
<div class="strattabletodivmobright">
<p class="stategybuysellchkbox clearfix">
<span class="BUY " name="spbuy">B</span>
<span class="SELL active" name="spsell">S</span>
</p></div>
You definitely don't need jQuery for this either
const activeText = Array.from(
document.querySelectorAll(".stategybuysellchkbox span.active"),
(span) => span.textContent
);

Search for Longest strings in Multi Blocks [duplicate]

This question already has answers here:
How to get the children of the $(this) selector?
(19 answers)
Closed 5 months ago.
I have 2 blocks of code and search for a longest string in each block appearing on each alert. obviously, wonderful is longest string in the block 1 and beautiful is longest string in block 2.
wonderful should be in first alert and beautiful should be in second alert, but somehow I miss something. Please give me a hand.
Thanks!
$('.parent').each(function() {
longest = "";
$('.child').each(function() {
var textChild = $(this).text();
if (textChild.length > longest.length) {
longest = textChild;
}
});
alert(longest)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
<div class="child">hello</div>
<div class="child">wonderful</div>
<div class="child">world</div>
</div>
<div class="parent">
<div class="child">hello</div>
<div class="child">beautiful</div>
<div class="child">world</div>
</div>
To do what you require you can use map() to build an array of the text content of the .child elements within each .parent, then you can use reduce() to get the longest of them.
Note that you need to output the longest value within the loop. Try this:
$('.parent').each(function() {
let longest = $(this).find('.child').map((i, el) => el.textContent.trim()).get().reduce((a, b) => a.length > b.length ? a : b);
console.log(longest);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
<div class="child">hello</div>
<div class="child">wonderful</div>
<div class="child">world</div>
</div>
<div class="parent">
<div class="child">hello</div>
<div class="child">beautiful</div>
<div class="child">world</div>
</div>
If you want to get the longest values outside of the loop, then I'd suggest pushing them to their own array which you can iterate through as required.

jQuery Relative path issue

I am currently am trying to get this below function working. I would like to use a relative path in order to add an active class for my different ul li a tag in my dom. The problem I am facing is that despite splitting the URL, it doesn't appear to be checking and comparing right part of the url and href. here is an of my HTML structure:
<div class="parent-bar"> <ul> <li>
<a class="parent-link" href="/dummy-parent-2/">
<div class="wrap">
<span class="text">Parent page 2
</span>
</div> </a> <div id="child-menu-1" class="child-menu">
<ul>
<li>
<a href="/dummy-parent-2/dummy-child-1/" class="child-links">
<div class="wrap">
<span class="text">child page 1
</span>
</div>
</a>
</li>
</ul> </div> </li> </ul> </div>
and here is the jQuery supposed to be tracking the url:
jQuery(function($) {
var path = window.location.href;
path = path.split("/");
$(".parent-link").each(function() {
if (this.href === path[3]) {
$(this).addClass('active');
}
});
if($('.parent-link').hasClass('active')){
$('.child-links').addClass('active');
}
});
The console is not showing any errors. I can see the active class is not added. The reason I am looking to do this is to allow the children of the parent items to be active as well. I will apply the same principle to the children to track the parent's respective URL. if that makes sense.
Any help or insight would be much appreciated.

XPath syntax for getting nodes after another node?

I have something like this:
<div id = "colors">
<div id = "color_red" ></div>
<div id = "color_blue" ></div>
<div id = "color_green"></div>
<div id = "color_black"></div>
<!-- and so on -->
</div>
I'm trying to select all the divs after the color_blue div with:
//div[#id="colors"]/following-sibling::div[#id="color_blue"]/div[starts-with(#id, 'color_')]
That doesn't work.
I also tried:
//div[#id="colors"]/div[starts-with(#id, 'color_')][following-sibling::div[#id="color_blue"]]
No luck with that either.
This XPath,
//div[#id="colors"]/div[#id="color_blue"]/following-sibling::div
will select all div siblings following the one with #id="color_blue" within the #id="colors" div.

How to find the text in all div elements without the first div coming from class "B"

I'm trying to capture the text inside all the div elements without the text of the first div that is the child of class "B"
I've been banging my head all day, but I can't seem to get it to work properly.
<div class="A">
Text 1
</div>
<div class="B">
<div>
Welcome 1
</div>
<div>
Welcome 2
</div>
</div>
This is the expression I'm using:
//body//text()[not (//div[#class='B']/div[1])]
but it is not returning any results.
After making the XML well-formed by giving it a single root element,
<div>
<div class="A">
Text 1
</div>
<div class="B">
<div>
Welcome 1
</div>
<div>
Welcome 2
</div>
</div>
</div>
Here are all the div elements that have no div descendents and are not the first div with a parent with a #class attribute value of `B':
//div[not(descendant::div) and not(../#class='B' and position() = 1)]
The above XPath selects these two div elements:
<div class="A">
Text 1
</div>
<div>
Welcome 2
</div>
So you can get the associated text() nodes using this XPath:
//div[not(descendant::div) and not(../#class='B' and position() = 1)]/text()
...which will select:
Text 1
Welcome 2
without selecting Welcome 1, as requested.