This question already has answers here:
Can I use an attribute selector for CONTAINS in queryselector()?
(2 answers)
CSS attribute selector class starts with but not equals to
(2 answers)
Closed 3 years ago.
Trying to get matches of attributes with wildcards to find out how many elements of a type are on the page
I think I should be able to use queryselectorAll on a partial attribute value as shown below, but no elements found :(
<div class="ag-right-align-cell" data-test="unitPrice&^%^&category=FRED&^%^&displayName=FREDS INFO;field=unitPrice&^%^&^%^&">$999.99</div>
<div class="ag-right-align-cell" data-test="unitPrice&^%^&category=GEORGE&^%^&displayName=GEORGES INFO;field=unitPrice&^%^&^%^&">$999.99</div>
var el=document.querySelectorAll('[data-test|=unitprice]');
console.log(el.length); // = 0
el.length shows 0 elements found.
What is the correct javascript for a match for selecting datatest=unitprice* ?
Related
This question already has answers here:
Get next / previous element using JavaScript?
(10 answers)
Closed 2 years ago.
How can I select the element after an element in JavaScript?
As example I need to add a class to the element after an element when hovering over it
All you need is CSS and the + adjacent sibling combinator
#target:hover + * {/*styles here*/}
By using JavaScript you can use Node.nextSibling
The next element is stored in nextElementSibling property of the specific element on javascript.
This question already has answers here:
jQuery clone duplicate IDs
(9 answers)
Does jQuery('#id').length always return 1?
(7 answers)
Closed 3 years ago.
on page load i have following div
<div id="row_log">......</div>
at this stage if i do console.log($('#row_log').length); then i get response as 1 which is correct.
however, i have button to clone the same div, which works fine. So if the button was clicked thrice, i would now have :
<div id="row_log">.....</div> original div
<div id="row_log">.....</div> cloned div
<div id="row_log">.....</div> cloned div
<div id="row_log">.....</div> cloned div
yet console.log($('#row_log').length); would still show as 1 instead of 4
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).
Use class.
Important points :
1. Id's must be unique.
2. In case of duplicate id's JavaScript will always get the first element for you, hence the length 1.
If you are just building a dynamic list or something, you can instead use a class.
......
And then do document.getElementsByClassName(className)
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.
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”].
This question already has answers here:
.getElementByClassName not working? [duplicate]
(2 answers)
Closed 7 years ago.
Im trying to make all my div class elements to lose all its content by replacing their current content with nothing "".
document.getElementsByClassName("sprint_column").innerHTML = "";
But nothing happens with the "sprint_column" when im trying to reach the class, However if i try to reach the IDs it works:
document.getElementsById("div3_Score").innerHTML = "";
Here is the code where the Ids and Classes is created:
<div class='sprint_column' id='div3_".$team."'>Sprint 1</div>
Is there any way to clear all content from a class with "getElementsByClassName" or do i have to loop through all ID elements and clear them one by one?
getElementsByClassName returns an array of DOM elements. You need to iterate through them, e.g. via a for loop, and change the innerHTML one by one.