How to find an element with specific data attribute value [duplicate] - javascript

This question already has answers here:
Filter divs by data-attr containing string
(4 answers)
Closed 6 years ago.
I'm filtering images based on data attributes, and for each filter data attribute there can be several tag values. To match an element based on one data attribute value, you can do this:
$('.image-container').filter('[data-places="Canada"]').css('display','none');
But when I try this with a space delimited list, it doesn't work:
<div class="image-container" data-places="Nunavut Canada">...</div>
Is there any way in jquery to select by a value in a set in a data attribute?
The other option I can think of is to loop through an array of selected elements, but I've got images loading in via ajax in an infinite scroll so this array would be very bulky at some point. I'm open to non-jquery solutions as well.

You have to use attribute contains a word selector at this context,
$('.image-container').filter('[data-places~="Canada"]').css('display','none');
Attribute equals selector cannot be used here since you are searching for a particular word in the attribute. Also you can use attribute contains selector, but that would select element with attribute like data-places="NunavutCanada"> also.

Related

querySelectorAll() targeting rel="me" attribute AND href attribute containing "12345" [duplicate]

This question already has answers here:
document.querySelector multiple data-attributes in one element
(3 answers)
Closed 2 years ago.
I have a very specific situation here that I can't find anywhere else.
I need to point to a <link> element that contains the rel="me" attribute AND an href="https://www.example.com/profile/12345" attribute that CONTAINS 12345.
<link rel="me" href="https://www.example.com/profile/12345"/>
I tried with document.querySelectorAll() but I don't know how to format it to make the AND operator work.
if (document.querySelectorAll('[rel="me"], a[href*=123456]')[0])
Any ideas on how to do this?
In a CSS selector "AND" is just not using a combinator at all.
If you have a type (or universal) selector, it has to go first.
The type selector to select <link> elements is link not a.
link[rel="me"][href*="12345"]
If you want the first match, use querySelector() instead of querySelectorAll()[0]
const el = document.querySelector('link[rel="me"][href*="12345"]');
console.log(el);
<link rel="me" href="https://www.example.com/profile/12345"/>

Why does document.getElementsByClassName return an array here? [duplicate]

This question already has answers here:
How to get element by class name? [duplicate]
(4 answers)
Get Element By Classname Script Not Working
(4 answers)
Closed 5 years ago.
Sorry for a stupid question, but maybe someone can explain it to me. On w3school web-site you can find a modal example. And in order to close the modal they use the following line of code:
HTML:
<span class="close">×</span>
Script:
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
Why to use array here? I tried this code without this array and it doesn’t work. I tried to use different indexes in this array and it also doesn’t work.
Why do we use [0] here and how does it exactly work?
According to Mozilla developer documentation, it returns an array of child elements.
Returns an array-like object of all child elements which have all of the given class names. When called on the document object, the complete document is searched, including the root node. You may also call getElementsByClassName() on any element; it will return only elements which are descendants of the specified root element with the given class names.
In your case, you have only one DOM element having class 'close'. That's why it returns an array of one element.
Because you can assign a class to more than one element in your HTML document. getElementsByClassName does exactly that: An array of all HTML elements which got the given class assigned to them. The [0] picks the first (and in your case only) element from that array.
If you want to give an unique identifier to a HTML element, assign an id to it and use getElementById.
<span id="close">×</span>
var span = document.getElementById("close");

Get element by a specific data attribute [duplicate]

This question already has answers here:
jQuery : select all element with custom attribute [duplicate]
(2 answers)
Closed 6 years ago.
Is there a way to grab a whole element by one of its data attributes via javascript or jQuery? Say i have a link somewhere on a website like this
Google
Since the link above doesn't have any id or class i cannot grab it by that. I need to get it by its data-source attribute. So say i could somehow with jQuery get the value of a attribute with the name of data-source. How would i do that ?
$('[data-source="google"]')
This will select all elements which data-source attribute equals to google.
See Attribute Equals Selector [name=”value”].

Find all elements with an 'id' attribute on the DOM [duplicate]

This question already has answers here:
jquery get only all html elements with ids
(3 answers)
Closed 8 years ago.
Simple. I want to traverse the DOM and find all elements that have an id attribute. Can someone help me with a js and/or jquery script
You can query all the elements with an id attribute by using the following jQuery selector:
$("[id]")
You can also just use plain JavaScript using querySelectorAll:
document.querySelectorAll("[id]")
If you want to find non-empty id attributes, use this selector instead:
'[id]:not([id]="")'
Simple attribute selector
$('[id]');
Reference : Has Attribute Selector [name]

Get Index Question - JQuery [duplicate]

This question already has answers here:
Select element by index (multiple elements of same class)
(4 answers)
Closed 2 years ago.
Possible Duplicate:
Select element by index (multiple elements of same class)
Quick question, I'm targeting all the article elements in my html5 page using:
var articles = $("article");
What I want to do is target one of the article elements using an index only. I can't seem to get it to work, any ideas?:
articles[1].css("display", "none"); // <-- This won't work
The array is returning the DOM element rather than the jQuery object. The .css() function does not exist on the DOM element, so you can wrap it in with the jQuery $ function to create a jQuery object that you can call .css() on.
Try $(articles[1]).css("display", "none");
Demo
Edit: Or even better articles.eq(1).hide();
You can use the .eq() function to target a specific index,
$("article").eq(1).css("display", "none");
According to the jQuery documentation referenced above,
Reduce the set of matched elements to
the one at the specified index.
Try this. This should target the first article
var articles = $('article').eq(0);
articles.css({"display":"none"});
Check this out for more of an explanation but this does exactly what you need.
http://api.jquery.com/eq/

Categories