How to filter html elements using Jquery function ?
Below is the html structure. I want to get the value of 2nd span which is inside a tag warpped in a li item ?
<li>
<a href="Link2">
<span class="message">Item2</span>
<span class="icon">Icon_2</span>
</a>
</li>
<li>
<a href="Link3">
<span class="message">Item3</span>
<span class="icon">Icon_3</span> ----- **How to filter and find this element value based on the message span value ?
</a>
</li>
```````````````
I need a condition like in li if span's value = "Item3", get the below span.icon's value
Couple of ways to do it. One way is with has and contains.
$("li:has(span.icon:contains('Icon_3'))").addClass("selected")
.selected {
background-color: lime;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>
<a href="Link2">
<span class="message">Item2</span>
<span class="icon">Icon_2</span>
</a>
</li>
<li>
<a href="Link3">
<span class="message">Item3</span>
<span class="icon">Icon_3</span>
</a>
</li>
</ul>
And now based on the information you provided.....
var text = $("span.message:contains(Item3) + span.icon").text()
console.log(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>
<a href="Link2">
<span class="message">Item2</span>
<span class="icon">Icon_2</span>
</a>
</li>
<li>
<a href="Link3">
<span class="message">Item3</span>
<span class="icon">Icon_3</span>
</a>
</li>
</ul>
or how most people would do it.
var messageElem = $("span.message").filter(function(){ return this.textContent === "Item3" });
var text = messageElem.next("span.icon").text();
console.log(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>
<a href="Link2">
<span class="message">Item2</span>
<span class="icon">Icon_2</span>
</a>
</li>
<li>
<a href="Link3">
<span class="message">Item3</span>
<span class="icon">Icon_3</span>
</a>
</li>
</ul>
I'd prefer this, because :contains() seeks the matching string and not exact string. That is why, using filter is better :)
var choice = $("li span.message").filter(function(){
return $(this).text().trim() === "Item3";
}).next('span.icon');
//If there is only one item with text "Item3"
console.log(choice.text());
//If there are multiple items
choice.each(function(){
console.log($(this).text());
});
.selected {
background-color: lime;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>
<a href="Link2">
<span class="message">Item2</span>
<span class="icon">Icon_2</span>
</a>
</li>
<li>
<a href="Link3">
<span class="message">Item3</span>
<span class="icon">Icon_3</span>
</a>
</li>
</ul>
Code:
$('li span.icon')
It should work
Related
I'm trying to change the active class of menu when I click on 'a' tag using jquery. I've searched a lot but none of them worked for me. when I click on a tag it redirects to 'href' attribute of the a tag and doesn't change the active menu.when I use 'e.preventDefult' it doesn't go to the 'href' but the active menu changes as I want.
What should I do?
This is the jquery I used:
<script>
$(document).ready(function(e) {
$('.navigation-menu-body ul li a').click(function (e) {
$('.navigation-menu-body ul li a.active').removeClass('active');
$(this).addClass('active');
$('.navigation-menu-body ul.navigation-active').removeClass('navigation-active');
$(this).parent().parent().addClass('navigation-active');
$('.navigation-icon-menu ul li.navigation-active').removeClass('active');
$('.navigation-icon-menu ul li').addClass('active');
window.location.href = $(this).attr('href');
console.log("href:", $(this).attr('href'));
e.preventDefault();
});
})
This is the html:
<div class="navigation">
<div class="navigation-icon-menu">
<ul>
<li data-toggle="tooltip" title="داشبورد">
<a href="#navigationDashboards" title="داشبوردها">
<i class="icon ti-pie-chart"></i>
</a>
</li>
<li data-toggle="tooltip" title="جشنواره های من">
<a href="#festivals" title="جشنواره های من">
<i class="icon ti-package"></i>
</a>
</li>
<li data-toggle="tooltip" title="کدهای جشنواره">
<a href="#serialNumbers" title="کدهای جشنواره">
<i class="icon ti-layers"></i>
</a>
</li>
<li data-toggle="tooltip" title="سوابق خرید">
<a href="#invoices" title="سوابق خرید">
<i class="icon ti-agenda"></i>
</a>
</li>
</ul>
</div>
<div class="navigation-menu-body">
<ul id="navigationDashboards" class="navigation-active">
<li class="navigation-divider">داشبورد</li>
<li>
<a class="active" href="/dashboard/index">فروش و مدیریت مشتری</a>
</li>
</ul>
<ul id="festivals">
<li class="navigation-divider">جشنواره ها</li>
<li>
جشنواره های من
</li>
</ul>
<ul id="serialNumbers">
<li class="navigation-divider">کدهای جشنواره</li>
<li>ثبت کد جدید </li>
<li>امتیازات من در هر اپلیکیشن </li>
</ul>
<ul id="invoices">
<li class="navigation-divider">سوابق خرید</li>
<li>خریدهای من </li>
</ul>
</div>
</div>
When clicking on an anchor tag, you goes to a different page, so in order for you to add an active class check the URL against the hrefs on document.ready and then add the class if the argument matches.
$document.ready(function() {
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,''));
$('.navigation-menu-body ul li a').each(function(){
if(urlRegExp.test(this.href)){
$(this).addClass('active');
}
});
});
I don't know much about jQuery but you can do it pretty easy in vanilla js.
First you specify an id for each li you got. then you need to check url so you can add active class for li tag of url you're currently in.for example:
<ul onload="checkUrl()">
<li id="tab1" data-toggle="tooltip" title="داشبورد">
<a href="#navigationDashboards" title="داشبوردها">
<i class="icon ti-pie-chart"></i>
</a>
</li>
<li id="tab2" data-toggle="tooltip" title="جشنواره های من">
<a href="#festivals" title="جشنواره های من">
<i class="icon ti-package"></i>
</a>
</li>
<li id="tab3" data-toggle="tooltip" title="کدهای جشنواره">
<a href="#serialNumbers" title="کدهای جشنواره">
<i class="icon ti-layers"></i>
</a>
</li>
<li id="tab4" data-toggle="tooltip" title="سوابق خرید">
<a id="tab4" href="#invoices" title="سوابق خرید">
<i class="icon ti-agenda"></i>
</a>
</li>
</ul>
then the js:
function checkUrl() {
if (document.location.href === "https://example.com/url-of-first-tab") {
document.getElementById("tab1").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-second-tab") {
document.getElementById("tab2").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-third-tab") {
document.getElementById("tab3").classList.add("active");
}
if (document.location.href === "https://example.com/url-of-forth-tab") {
document.getElementById("tab4").classList.add("active");
}
}
Also keep it in mind that none of your a tags shoud have active class in html!
I want to remove time + icon shown in my blog page
<span><i class="fa fa-newspaper-o"></i>47 sec read</span>
I have tried using
.fa-newspaper-o{display:hidden}
but it only hides the icon before "47 sec" leaves the text displayed
Whole code
<ul class="list-inline">
<li><span class="posted-in"> <a class="national" href="http://www.fact-file.com/category/national/?customize_changeset_uuid=2a4ce41e-5aad-4e11-bb78-50c7e06dd416&customize_messenger_channel=preview-0" rel="category tag" style="background-color: #1abc9c">National</a>
<a class="top" href="http://www.fact-file.com/category/top/?customize_changeset_uuid=2a4ce41e-5aad-4e11-bb78-50c7e06dd416&customize_messenger_channel=preview-0" rel="category tag" style="">Top Story</a>
</span></li>
<li>
<span class="right"><span class="zilla-likes-count">0</span> <span class="zilla-likes-postfix"></span></span>
</li>
<li>
<span class="post-comments-number">
<i class="fa fa-comment-o"></i>0 </span>
</li>
<li><span><i class="fa fa-newspaper-o"></i>47 sec read</span></li>
</ul>
.list-inline li:nth-child(4) { display: none;} or .list-inline li:last-child { display: none;} will hide the list item in question.
To add to the above, you can use visibility: hidden if you want the removed element to still leave a footprint (take up space).
var li=document.getElementsByTagName('li');
li[li.length-1].style.visibility='hidden';
<ul class="list-inline">
<li><span class="posted-in"> <a class="national" href="http://www.fact-file.com/category/national/?customize_changeset_uuid=2a4ce41e-5aad-4e11-bb78-50c7e06dd416&customize_messenger_channel=preview-0" rel="category tag" style="background-color: #1abc9c">National</a>
<a class="top" href="http://www.fact-file.com/category/top/?customize_changeset_uuid=2a4ce41e-5aad-4e11-bb78-50c7e06dd416&customize_messenger_channel=preview-0" rel="category tag" style="">Top Story</a>
</span></li>
<li>
<span class="right"><span class="zilla-likes-count">0</span> <span class="zilla-likes-postfix"></span></span>
</li>
<li>
<span class="post-comments-number">
<i class="fa fa-comment-o"></i>0 </span>
</li>
<li><span><i class="fa fa-newspaper-o">47 sec read</i></span></li>
</ul>
The Code:
<ul class="site-nav list--inline " id="SiteNav">
<li class="site-nav--active">
<a href="/" class="site-nav__link>
<span class="site-nav__label">HOME</span>
</a>
</li>
**<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">ORDER NOW</span>
</a>
</li>**
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">TRACK ORDER</span>
</a>
</li>
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">HELP CENTER</span>
</a>
</li>
</ul>
I want to target the SECOND span tag in the list with class: site-nav__label using pure CSS.(nth-child, second-of-type etc.)
I cannot add ids or classes to this span tag.
Help will be greatly appreciated.
For select the second li try with
#SiteNav li:nth-of-type(2)
and then for select the span of second li add span after:
#SiteNav li:nth-of-type(2) span
Because you want inside ul tag, I used ul.site-nav for ul tag with class site-nav
ul.site-nav li:nth-of-type(2) a > span {
border: 3px solid red;
}
<ul class="site-nav list--inline " id="SiteNav">
<li class="site-nav--active">
<a href="/" class="site-nav__link>
<span class="site-nav__label">HOME</span>
</a>
</li>
**<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">ORDER NOW</span>
</a>
</li>**
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">TRACK ORDER</span>
</a>
</li>
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">HELP CENTER</span>
</a>
</li>
</ul>
To Target the span use a combination of nth-of-type and child combinators
#SiteNav > li:nth-of-type(2) > a > span {
font-weight:bold;
color:red;
}
/*Or Target the span class*/
#SiteNav > li:nth-of-type(2) .site-nav__label {
font-style:italic;
}
<ul class="site-nav list--inline " id="SiteNav">
<li class="site-nav--active">
<!-- Fixed Missing closing " -->
<a href="/" class="site-nav__link">
<span class="site-nav__label">HOME</span>
</a>
</li>
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">ORDER NOW</span>
</a>
</li>
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">TRACK ORDER</span>
</a>
</li>
<li>
<a href="/" class="site-nav__link">
<span class="site-nav__label">HELP CENTER</span>
</a>
</li>
</ul>
For a good article on nth-child (and nth-of-type) see https://css-tricks.com/useful-nth-child-recipies/
I am fairly new to HTML coding, so could really use the communities help here. I have a HTML RichText widget, which I am using to create a navigation bar within our data visualization tool, Datorama. Unfortunately, when you click on the menu items, it keeps opening the page within the widget and not loading the full page.
Here is the Code:
<nav class="navigation">
<ul class="menu">
<li class="menu__item">
<a href="#" class="menu__link">
<span class="menu__title">
<span class="menu__first-word" data-hover="About">
About
</span>
<span class="menu__second-word" data-hover="Us">
Us
</span>
</span>
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link">
<span class="menu__title">
<span class="menu__first-word" data-hover="Our">
Our
</span>
<span class="menu__second-word" data-hover="History">
History
</span>
</span>
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link">
<span class="menu__title">
<span class="menu__first-word" data-hover="Latest">
Latest
</span>
<span class="menu__second-word" data-hover="News">
News
</span>
</span>
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link">
<span class="menu__title">
<span class="menu__first-word" data-hover="New">
New
</span>
<span class="menu__second-word" data-hover="Arrivals">
Arrivals
</span>
</span>
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link">
<span class="menu__title">
<span class="menu__first-word" data-hover="On">
On
</span>
<span class="menu__second-word" data-hover="Sale">
Sale
</span>
</span>
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link">
<span class="menu__title">
<span class="menu__first-word" data-hover="Contact">
Contact
</span>
<span class="menu__second-word" data-hover="Us">
Us
</span>
</span>
</a>
</li>
</ul>
Could anyone please assist in adjusting this to when a menu item is clicked, it opens a new page?
Thank you,
-Nakul B.
I'm Johnny from Datorama's Tier 2 Support,
Because the Rich Text works with iFrames, it means that the browser opens a new embedded HTML document within the active one, and links are opened within the embedded iFrame instead of the tab itself, or a new tab.
This can be controlled by working with the a tag's 'target' attribute, and the following options can be assigned:
_blank - new window or tab
_self - Opens within the same window or iFrame (default behaviour)
_parent - Opens the link within the parent frame
_top - Opens within the full body of the window
In the case mentioned, we should use the '_blank' attribute, and the following code should be used:
<nav class="navigation">
<ul class="menu">
<li class="menu__item">
<a href="https://google.com" class="menu__link" target="_blank">About Us
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link" target="_blank">Our History
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link" target="_blank">Latest News
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link" target="_blank">New Arrivals
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link" target="_blank">On Sale
</a>
</li>
<li class="menu__item">
<a href="#" class="menu__link" target="_blank">Contact Us
</a>
</li>
</ul>
I've also changed the structure of the a tags, and reduced the number of span within it, to increase readability. Now the links should open a new page/tab on click.
Please don't hesitate to contact us at support#datorama.com for further assistance for setting your widget up!
I'm using bootstrap list-inline class to style my breadcrumbs but I don't like the spacing between the elements. How do I reduce the spacing between John, Jane and David ?
Here's how it currently looks
<ul class="list-inline">
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="http://www.google.com" itemprop="url">
<span itemprop="title">John</span>
</a> >
</div>
</li>
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="www.yahoo.com" itemprop="url">
<span itemprop="title">Jane</span>
</a> >
</div>
</li>
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="www.msn.com" itemprop="url">
<span itemprop="title">David</span>
</a>
</div>
</ul>
</li>
You can override the base css and add a negative margin to the li elements like so:
.list-inline>li {
margin-right: -10px;
}
Run the below code snippet to see what this produces.
.list-inline>li {
margin-right: -10px;
}
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<ul class="list-inline">
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="http://www.google.com" itemprop="url">
<span itemprop="title">John</span>
</a> >
</div>
</li>
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="www.yahoo.com" itemprop="url">
<span itemprop="title">Jane</span>
</a> >
</div>
</li>
<li>
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="www.msn.com" itemprop="url">
<span itemprop="title">David</span>
</a>
</div>
</li>
</ul>
One of the options is to remove the padding from elements with css.
.no-left-gutter{padding-left:0;}
And add this class to li or a elements.
<li class="no-left-gutter">
<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb">
<a href="http://www.google.com" itemprop="url" class=no-left-gutter>
<span itemprop="title">John</span>
</a>
</div>
</li>
can be fixed by funky code formatting...
<ul class="list-inline">
<li>
List Item One</li><li>
List Item Two</li><li>
List Item Three</li>
</ul>
can be fixed by adding html comments...
<ul class="list-inline">
<li>List Item One</li><!--
--><li>List Item Two</li><!--
--><li>List Item Three</li>
</ul>