Select an element after another element in JavaScript [duplicate] - javascript

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.

Related

How do i append a div using javascript and not jquery? [duplicate]

This question already has answers here:
Create <div> and append <div> dynamically
(9 answers)
Closed 2 years ago.
I used a query selector to get the div i want to append. But the created div doesn't want to append to anything other than the body. Is there a way i can make it specifically append under the div?
Look at this: https://www.w3schools.com/jsref/met_node_appendchild.asp
What you need to do is
parent.appendChild(thatDiv);

Matching partial Attribute values with selector [duplicate]

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* ?

Can I access the ::before element with jQuery / JavaScript? [duplicate]

This question already has answers here:
Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)
(26 answers)
Closed 5 years ago.
I made this function to "split" my elements and give them background-colors:
var colors = ['red','yellow','green','blue'];
function splitColors($item) {
$item.each(function(i) {
$(this).addClass(colors[i % 4]);
});
}
splitColors($('.comment:before'));
now I want to apply this function on a before element in my DOM, but it doesn't work.
Can I access the ::before element in jQuery?
I can't use a real element, cause the WordPress comment section don't allow own markup..
Thanks!
I think this works:
splitColors($('.comment').before());

Clear all the contet in class elements using getElementsByClassName [duplicate]

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.

Using :not() with attributes [duplicate]

This question already has answers here:
How can I get the elements without a particular attribute by jQuery
(7 answers)
Closed 9 years ago.
I'm trying to use jquery to grab all select elements which do not have the "multiple" attribute set. I tried with the :not() but I can't get it quite right.
$(document).ready(function() {
$('select:not(multiple)').select2();
});
You forgot the attribute selector. What you're looking for there is "a <select> that is not a <multiple>"
$("select:not([multiple])").select2();
$('select:not([multiple])').select2();
or
$('select').not('[multiple]').select2();

Categories