Selecting children elements from a <ul> - javascript

So I want to select the "href" attribute from all the element that are in a list. So in my case I want to get link from the a element.
So I've trief 2 diffrent each loops.
Html and JS Example:
<ul id="product_list">
<li>
<div class="class1">
<div class="class2>
<div class="class3">
<span class="productPic"></span>
<a class="link" href="www.THISLINK.com"></a>
</div>
<div class="title"></div>
<div class="logos"></div>
</div>
</div>
</li>
<li>
<div class="class1">
<div class="class2>
<div class="class3">
<span class="productPic"></span>
<a class="link" href="www.THISLINK.com"></a>
</div>
<div class="title"></div>
<div class="logos"></div>
</div>
</div>
</li>
<li>
<div class="class1">
<div class="class2>
<div class="class3">
<span class="productPic"></span>
<a class="link" href="www.THISLINK.com"></a>
</div>
<div class="title"></div>
<div class="logos"></div>
</div>
</div>
</li>
</ul>
let $ = cheerio.load(resp);
$('#product_list > li').each((index,element) => {
console.log($this).children('.link').attr('href');
}
$('.link').each((index,element) => {
console.log($this).attr('href')
}
I'm only getting 'undefined' as result.

$('#product_list .link').each(function(index, el){console.log($(el).attr('href'))})

First of all, it is called $(this) and not $this.
This change will at least give you all the results you want. If you now just want the results that actually contain information, you should either use the second each loop or you can use $('[href]').

With map and es6 you would do:
const hrefs = $('#product_list > li .link').map((i, el) => el.attr('href')).get()
Note the get() at the end which turns it into a js array

Related

Why is my website displaying <ul> and <li> in the dom

I'm trying to display cards in my dom but in the text im showing there is some code as shown in the picture below
<div class="app">
<h1> All Fishes and Their Photos</h1>
<ul v-for="(fish, i) in fishes" :key="i" />
<li>
<div class="maincontainer">
<div class="back">
<p>{{fish['Biology']}}</p>
</div>
<div class="front">
<div class="image">
<img :src="fish['Species Illustration Photo'].src" />
<h2>{{ fish['Species Name']}}</h2>
</div>
</div>
</div>
</li>
</ul>
</div>
Remove the '/' you have at the end of the first <ul> tag, this should fix the problem, but also consider using the v-for on the <li> tag, and not the <ul> tag as
the element that must be repeated many times is <li>.

each() if number of elements are present in each div, set class remove to other div

I have a search result page. Each article there has an image, title, description and tags. I want that each article which has less than 6 tags, to add remove class from a div.
$('.searchTag-container').each(function(){
cat = $('.searchResult__tag',this).children().length;
if (cat < 6) {
console.log(cat);
$(".share").addClass("remove");
}
});
When I do this the remove class adds to all of articles even if it has 6 or more tags.
<div class="row bottom">
<div class="col-md-8">
<div class="searchResults">
<div class="searchTag-container">
<div class="searchResult__tag " data-number="0">
Tag1
</div>
<div class="searchResult__tag" data-number="1">
Tag2
</div>
<div class="searchResult__tag" data-number="2">
Tag3
</div>
<div class="searchResult__tag" data-number="3">
Tag4
</div>
<div class="searchResult__tag" data-number="4">
Tag5
</div>
<div class="searchResult__tag" data-number="5">
Tag6
</div>
</div>
</div>
</div>
<div class="col-md-4 share">
<div class="social">
<a class="btn-facebook"><img src="""></a>
<a class="btn-twitter"><img src=""></a>
<a class="btn-linkedin"><img src="""></a>
</div>
</div>
Your code is working ok, but the mistake is you are looping through .searchTag-container, which will iterate only once and you are checking .searchResult__tag's length,
so in .searchTag-container there are 6 .searchResult__tag and each one has one children, so it gives length of all .searchResult__tag's children combined so it becomes 6, and so variable cat = 6 and it gives false when you check cat < 6.
So for solving it, you have to iterate through loop of each .searchResult__tag and check it's children length as i have done,
check below for working solution of your problem:
$(function(){
$('.searchTag-container .searchResult__tag').each(function(){
cat = $(this).children().length;
if (cat < 6) {
console.log(cat);
$(".share").addClass("remove");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row bottom">
<div class="col-md-8">
<div class="searchResults">
<div class="searchTag-container">
<div class="searchResult__tag " data-number="0">
Tag1
</div>
<div class="searchResult__tag" data-number="1">
Tag2
</div>
<div class="searchResult__tag" data-number="2">
Tag3
</div>
<div class="searchResult__tag" data-number="3">
Tag4
</div>
<div class="searchResult__tag" data-number="4">
Tag5
</div>
<div class="searchResult__tag" data-number="5">
Tag6
</div>
</div>
</div>
</div>
<div class="col-md-4 share">
<div class="social">
<a class="btn-facebook"><img src=""></a>
<a class="btn-twitter"><img src=""></a>
<a class="btn-linkedin"><img src="""></a>
</div>
</div>
you can try below script:
$('.searchTag-container').each(function(){
cat = parseInt($('.searchResult__tag',this).children().length);
if (cat < 6) {
$(this).closest(".row").children(".share").addClass("remove");
console.log(cat);
}
});
check this fiddle
Hope this will help you.
To select the Children with a specific class I would use $(this).children('.searchResult__tag') and get the length of that by .length as you did before

Recovering multi id at a time

I try to put in place a system popup, why I decided to use the data attribute.
I wish that when an element has the id "popup" I recovered using the jQuery data-item value.
The data-item value has the id to display in my popup, my problem is that if I have several id popup, recovered came only 1 single id, the first in this page.
How can I do to retrieve all the popup and display ID is the corresponding value with the data-item?
<div id="boxpop">
<div class="centered">
<span></span>
<div class="close-btn"></div>
</div>
</div>
<div id="login" style="display:none;">
Test div
</div>
<header>
<div id="MainHeader">
<div class="logo"></div>
<nav id="Menu">
<li>
<span class="icon"></span>
<span class="text">Click me!</span>
</li>
<li>
<span class="icon icone card"></span>
<span class="text">Shop info</span>
</li>
</nav>
</div>
</header>
<script type="text/javascript">
$("#popup").click(function() {
a = $("#popup").data("item");
alert(a);
});
function demo(div) {
$("#boxpop").fadeIn(500);
$("#boxpop .centered span").empty();
$(div).insertAfter("#boxpop .centered span").fadeIn(100);
}
</script>
ID must be unique.
You should use class for this, this is the correct way:
<li>
<span class="icon"></span>
<span class="text">Click me!</span>
</li>
<li>
<span class="icon icone card"></span>
<span class="text">Shop info</span>
</li>
And then...
$(".popup").click(function() {
a = $(this).data("item");
alert(a);
});
Hope it helps.
The is is because cannot have multiple elements with the same id attribute in the same document - it's invalid HTML. You should use classes instead. From there you can use the this keyword in the click handler to reference the element which raised the event and read the data attribute. Try this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="boxpop">
<div class="centered">
<span></span>
<div class="close-btn"></div>
</div>
</div>
<div id="login" style="display:none;">
Test div
</div>
<header>
<div id="MainHeader">
<div class="logo"></div>
<nav id="Menu">
<li>
<span class="icon"></span>
<span class="text">Click me!</span>
</li>
<li>
<span class="icon icone card"></span>
<span class="text">Shop info</span>
</li>
</nav>
</div>
</header>
<script type="text/javascript">
$(".popup").click(function() {
a = $(this).data("item");
alert(a);
});
function demo(div) {
$("#boxpop").fadeIn(500);
$("#boxpop .centered span").empty();
$(div).insertAfter("#boxpop .centered span").fadeIn(100);
}
</script>

Manage complex select/deselect artis/album/song

First of all sorry for the vague title, but i didn’t know how to explain my problem in a better way.
Anyway this is what i want. Let’s say we have three tabs: one showing artist list, one showing album list and one showing songs. All this three tabs has selectable lists and i would like to be able, for example, to select and artist, then going to albums (that now should be show all as selected because i checked the artis) and be able to deselect one album and then, for, example, be able to go to songs and manage the songs individually.
Of course then i should be able to retrive the list of all the songs selected by the user.
EDIT 1
added two images to explain better
EDIT 2
Added some code. At the moment i have only HTML and CSS, i'm waiting for your help for the JS code :)
that's the HTML structure
<div id="tabs" class="page-content tabs overflowTab">
<div id="tab1" class="tab active">
<div class="content-block-title" id="indexScrollOffset">Seleziona artisti</div>
<div class="list-block media-list">
<ul>
<li>
<div class="item-content.modificato">
<div class="item-media modificato">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="#" class="item-link" id="apriTitoliLibro1">
<div class="item-inner modificato">
<div class="item-title-row">
<div class="item-title">Artist 1</div>
</div>
<div class="item-subtitle">Some Text</div>
</div>
</a>
</div>
</li>
<li>
<div class="item-content.modificato">
<div class="item-media modificato">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="#" class="item-link" id="apriTitoliLibro2">
<div class="item-inner modificato">
<div class="item-title-row">
<div class="item-title">Artist 2</div>
</div>
<div class="item-subtitle">Some Text</div>
</div>
</a>
</div>
</li>
[...]
</ul>
</div>
</div>
<div id="tab2" class="tab">
<div class="content-block-title">Seleziona Album</div>
<div class="list-block media list">
<div class="list-group">
<ul>
<li class="list-group-title">Artist 1</li>
<li id="titoliLibro1">
<div class="item-content-modificato titoli">
<div class="item-media modificato fake-media-list">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="personalizzaArticoli.html" class="item-link">
<div class="item-inner modificato titoli">
<div class="item-title-row modificato">
<div class="item-title modificato">Album 1</div>
<div class="item-after modificato"><span class="font-size-artt-label">Some text</div>
</div>
</div>
</a>
</div>
</li>
<li>
<div class="item-content-modificato titoli">
<div class="item-media modificato fake-media-list">
<i class="icon icon-form-checkbox modificato"></i>
</div>
<a href="personalizzaArticoli.html" class="item-link">
<div class="item-inner modificato titoli">
<div class="item-title-row modificato">
<div class="item-title modificato">Album 2</div>
<div class="item-after modificato"><span class="font-size-artt-label">Some text</div>
</div>
</div>
</a>
</div>
</li>
[...]
</ul>
</div>
</div>
</div>
<div id="tab3" class="tab">
<div class="content-block-title">Seleziona song</div>
<div class="list-block searchbar-not-found">
<ul>
<li class="item-content">
<div class="item-inner">
CONTENT HERE IS ADDED DYNAMICALLY FROM A WEBSQL DB
</div>
</li>
</ul>
</div>
</div>
Edit 3
It's a phonegap application and yes, already using jQuery (as less as possibile for performance) :)
Now my question: which is the best way to handle this? My only idea is to create an array and update it with all the elements selected and, in order to show an element as selected or not, checking it with indexOf to see if it exist… is this a good way? I’m a bit worried about performance.
Thanks

jQuery find closest ul

My HTML structure is like this:
<div class="product-slider-title">
<div><span class="left"> </span></div>
<div>Sample title</div>
<div><span class="right"> </span></div>
</div>
<div id="prslider1" class="product-slider-wrapper">
<div class="left-prduct-slider-nav sprites"></div>
<div class="right-prduct-slider-nav sprites"></div>
<div class="product-slider">
<ul>
<li class="product-holder"></li>
</ul>
</div>
</div>
How can i find the closest .product-slider ul from .left-prduct-slider-nav when i click on .left-prduct-slider-nav?
if this is .left-prduct-slider-nav then
var ul = $(this).siblings('.product-slider').find('ul')
Demo: Fiddle
You can use nextAll() to search the siblings after current element and use find() to searched the children in the matched element returned by nextAll.
Live Demo
$(this).nextAll('.product-slider').find('ul');
This should work
var ul = $('.left-prduct-slider-nav').closest('ul');
Hmm try this:
$('.left-prduct-slider-nav').click(function () {
var closest_ul = $(this).parent().find('.product-holder ul');
});
The closest .product-slider ul from .left-prduct-slider-nav is just the first one in the list, so you can use :first:
$('.left-prduct-slider-nav').click(function() {
$(this).parent().find('.product-slider ul:first')
})
Try first-child:
HTML:
<div class="product-slider-title">
<div><span class="left"> </span>
</div>
<div>Sample title</div>
<div><span class="right"> </span>
</div>
</div>
<div id="prslider1" class="product-slider-wrapper">
<div class="left-prduct-slider-nav sprites">Left Slider Nav</div>
<div class="right-prduct-slider-nav sprites"></div>
<div class="product-slider">
<ul>
<li class="product-holder1"></li>
</ul>
<ul>
<li class="product-holder"></li>
</ul>
<ul>
<li class="product-holder"></li>
</ul>
<ul>
<li class="product-holder"></li>
</ul>
</div>
</div>
<div class="product-slider-title">
<div><span class="left"> </span>
</div>
<div>Sample title</div>
<div><span class="right"> </span>
</div>
</div>
<div id="prslider1" class="product-slider-wrapper">
<div class="left-prduct-slider-nav sprites"></div>
<div class="right-prduct-slider-nav sprites"></div>
<div class="product-slider">
<ul>
<li class="product-holder"></li>
</ul>
</div>
</div>
JS:
$(document).ready(function () {
$(".left-prduct-slider-nav").click(function () {
alert($(".product-slider").find("ul:first-child").find("li").prop("class"));
});
});
jsFiddle.
Explanation:
I handle the .click() function of .left-prduct-slider-nav then I alert the class of the closest or in other words the first ul:first-child's li.
Proof:
The proof is that I changed the class of the first ul's li and then alert() it and then the output I get is this: product-holder1.
Try this:
$(this).next().next().find('ul');

Categories