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

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

Related

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>

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

Setting a radio value to yes upon checking another field [duplicate]

This question already has answers here:
Check a radio button with javascript
(7 answers)
Closed 5 years ago.
Quick question I'm unable to resolve... I have a radio button with values of yes or no.
Basically I'm doing this:
if (complete === '1') {
getElementById('test-radio').value('Yes')
}
But it's not setting this to yes, it's still defaulting as no...
Any advice on how I can make this work?
You can use either radioButton.checked=true;
What you are doing isn't javascript syntax.
if (complete === '1') {
getElementById('test-radio').checked=true;
}

Destroy DIVs with their IDs containing a particular string [duplicate]

This question already has answers here:
How to find if div with specific id exists in jQuery?
(10 answers)
Closed 7 years ago.
I have some DIVs with IDs like 'abc_chosen', 'danger_chosen', etc.
I would like to delete all DIVs when their IDs contain the string '_chosen'.
How can I do that?
Try this:
$('div[id$="_chosen"]').remove();
$ here will check if _chosen is at the end of id.
Thanks to #evolutionxbox:
If you want to check if id contain _chosen anywhere(not at the end)
$('[id*="_chosen"]').remove();
Docs: https://api.jquery.com/attribute-ends-with-selector/

Is there a JavaScript alternative to isNaN()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript logic for isNaN()
Hey all I want is to check that the entered value in a textbox is not a number or without using the function isNaN();. Is there any way?
parseFloat(value)!== +value;
But what is wrong with isNaN(value)?

Categories

Resources