How to get an H1 using getelementby [duplicate] - javascript

This question already has answers here:
Javascript getElementsByTagName
(4 answers)
Closed 8 years ago.
I have this code which I've been trying to fix for hours.
<div class="product_text">
<h2>empty</h2>
<h3>empty</h3>
<h1>THIS</h1>
</div>
I have tried everything: getElementsByTagName, getElementsByID, getElementsByClassName,
return $dom_document->getElementByTagName("h1")->nodeValue;;
But no success.

You can do it by
var h1s = document.getElementsByTagName('h1');
alert(h1s[0].innerHTML);
Demo Fiddle

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>

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

JavaScript querySelectorAll classname innerHTML [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 5 years ago.
I have following element :
document.querySelectorAll('.apply-dealer-xyz').innerHTML = "Deselect";
<button class="btn cta-primary apply-dealer-xyz">Select</button>
But it is not working.
Anybody have some guess?
And another question it is possible do something like this in jquery? (pseudo) :
document.querySelectorAll('[classname*=apply-dealer]').innerHTML = "anything"
That will change every element containing apply-dealer in class name?
Thanks,
document.querySelectorAll('.apply-dealer-xyz')[0].innerHTML = "Deselect";
You have to use innerHTML on first element of querySelectorAll, because querySelectorAll returns array
document.querySelectorAll('.apply-dealer-xyz')[0].innerHTML = "Deselect";
<button class="btn cta-primary apply-dealer-xyz">Select</button>

Extract Custom HTML Attribute Value for Javascript Variable [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 5 years ago.
I'm looking at three custom HTML attributes within an "a" tag and I'd like to extract their values into 3 separate javascript variables.
Here's the HTML with the attributes "data-event-category", "data-event-action", and "data-event-label":
'<a data-event-category="Billionaire" data-event-action="SeeMore" data-event-label="Biography" href="xxx" class="ga-track-click billionaires-individual-see-more gotham-medium red-txt">Contact us to see more Biography</a>'
And this is the function I cobbled together, unsuccessfully trying to extract the value of "data-event-category":
`function myFunction3() {
var z = document.getElementByClass(".ga-track-click").getAttribute("data-event-category");
return z;
}`
Here's my fiddle :
https://jsfiddle.net/comicosp/430350g0/#&togetherjs=tGRIiss2gB
Can you please tell me the** correct way to extract the values of the 3 custom HTML attributes**?
** DUPLICATE ISSUE**
I can see how :
What do querySelectorAll, getElementsByClassName and other getElementsBy* methods return? would be similar, but I'm not sure how it applies to my question. I'm very very beginner with javascript.
Change
getElementByClass
to
getElementByClassName
or use
querySelector

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