Javascript new feature? [duplicate] - javascript

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>

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.

find if html class exist [duplicate]

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

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

Determine if a variable is an empty jquery object [duplicate]

This question already has answers here:
How can I detect if a selector returns null?
(8 answers)
Closed 8 years ago.
Setup:
<div>
<div class="application-section active"></div>
<div class="application-section"></div>
</div>
I am setting a the following variables
var $activeSection = $("div.application-section.active");
var $targetSection = $activeSection.prev("div.application-section");
jQuery Documentation states that if no previous sibiling is found, it will return a blank jquery object. I want to do additional stuff if the variable is a blank jQuery object, but how do you check to see if it is a blank jQuery object? I have tried the following, but I always get false as a result.
alert($.isEmptyObject($targetSection));
$targetSection.length will be zero if no matching selector is found.

Categories

Resources