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

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());

Related

How can I do jquery's parent() in JavaScript? [duplicate]

This question already has answers here:
Getting the parent div of element
(7 answers)
Add CSS attributes to element with JavaScript
(5 answers)
Set CSS property in JavaScript?
(11 answers)
Closed 3 months ago.
I'm trying to translate this jQuery into JavaScript, to save a bit on load time. Here's the line of jQuery:
$('div.annotated p > span.citation').parent().css('display', 'inline')
And here's what I have so far in JavaScript:
document.querySelectorAll('div.annotated p > span.citation').forEach((el) => {})
I'm stuck on how to find the parent of an element, and also apparently how to set the CSS for that element. Should I stick with jQuery, or can JavaScript also do this, and is it much harder?

How can I call the ::before in JavaScript by DOM style [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 2 years ago.
I need to change pseudo element (::before) style in javaScript by DOM style. How can I do that?
Like this
document.getElementById("new-store-nav").style.color= "#000";
I have changed this style by calling ID but how can I call ::before in javascript?
You need to use getComputedStyle function which is on window object
In your case it would be like:
const color = window.getComputedStyle(
document.getElementById("new-store-nav"), ':before'
).getPropertyValue('color')
Reference to David Walsh's site here

Get css values of a hover element in an iframe [duplicate]

This question already has answers here:
Access the css ":after" selector with jQuery [duplicate]
(3 answers)
Closed 8 years ago.
I can get css of an element in an iframe by this way:
var bg_color = $('iframe.#myIframe').contents().find('div#someId').css('background-color');
but I can't get:
var bg_color = $('iframe.#myIframe').contents().find('div#someId:hover').css('background-color');
So, how can I get css of a hover element in an iframe (same domain)? Please help me! Thanks!
This cannot be done, the iFrame is literally a window to another webpage, therefor it is not in your DOM, and not accessible by your jquery

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();

Raw javascript equivalent to jQuery.empty() [duplicate]

This question already has answers here:
Remove all child elements of a DOM node in JavaScript
(37 answers)
Closed 9 years ago.
I found some jQuery code to clear the contents of a div:
$('#masterdiv').empty();
Is there any similar way to do this in raw javascript, without jQuery or any other library?
This code will work.
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
Mention the id of the tag on myNode for which you want to remove the inner child.
Please have a look on
http://jsfiddle.net/2dJAN/19/
<div class="btn-post">123</div>
$(".btn-post").html("")

Categories