find if html class exist [duplicate] - javascript

This question already has answers here:
How to get element by class name? [duplicate]
(4 answers)
Is there a way to find out where a css rule is coming from?
(3 answers)
Get Stylesheet Rule Javascript / jQuery
(1 answer)
Closed 9 months ago.
I have a class:
<style>
.jonny {
color:"red"
}
</style>
in my element:
<div class=jonny>abc</div>
I want to check via JavaScript if class jonny have a content. sometime I want even to check where in my source it define. In which .css file it define.
I need to do it in runtime = in JavaScript.
Thank you
j.r

Related

Why/how is each div with an `id` callable from js? [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Closed 9 months ago.
While coding some js for a webpage, I had initially written a function to parse every div with an id and assign it to a variable for easy targeting:
var div={};
for (let e of Array.from(document.getElementsByTagName('div'))
.filter(div => div.id)) {
div[e.id] = e;
};
But while debugging I suddenly realized that js recognized all these divs "out of the box" and that I could call them directly. For example, if my HTML contains
<div id='status'>
<div id='modtime'></div>
<div id='client'></div>
</div>
Then I can simply do modtime.innerText='abc' – without having to assign modtime via something like div.modtime = document.getElementById('modtime')
Would like to know more about this 'functionality'; cannot find any documentation on it. Not sure what it's called.

Javascript new feature? [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Closed 9 months ago.
I wanted to know if its new feature or just a working bug
Before I used to do like
HTML :
<div id="identity">I am an demo text</div>
JS :
let iddiv = document.getElementById("identity");
console.log(iddiv)
but Now I can do
console.log(identity)
Withour storing the element in a variable
I just wanted to ask if it is fine !!
console.log(identity)
<div id="identity">I am an demo text</div>

Show or hide class as a parameter [duplicate]

This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 3 years ago.
This is basic ,
I can show or hide a class with :
$('.blink').show();
but I would like to do the same with a parameter so
var a = "blink";
$(a).show();
will not work obviously and I couldn't find the keywords to look for this in google.
You need to append a dot when you use that variable so that the $ will consider it as a class selector:
var a = "blink";
$('.'+a).show();

The right way to check if an html element exist [duplicate]

This question already has answers here:
How can I check if an element exists in the visible DOM?
(27 answers)
Closed 4 years ago.
What is the proper way to check if an html element exist in a webpage.
I do like this:
if (document.getElementById('container') !== null) {doThis()}
How good is this?
What are other ways?
Answered
I was looking for
document.body.contains(element))

What does the * do when used as part of an attribute selector? [duplicate]

This question already has answers here:
What is this CSS selector? [class*="span"]
(5 answers)
Closed 6 years ago.
I'm looking at the following code:
var script = document.querySelector('script[src*="' + file + '"]');
What does the * component of the selector do?
* at attribute selector matches file if file string is contained within the attribute

Categories

Resources