JavaScript - getting text from <strong> or div by class [duplicate] - javascript

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 years ago.
I wonder how to get data from this code. I want to download these numbers, but I don't know how.
I have already tried such methods but they did not work.
document.querySelector('.myclass');
document.querySelector('.myclass strong');
document.querySelectorAll('.myclass strong');
And this is the element from which I want to get the numbers. There are two ways on the site but I don't know which will be better so I send 2 items with numbers
<div class="myclass" role="button" tabindex="0">Next number: <strong>97554</strong></div>
or
<div class="anyclass">97532</div>

Did you mean
document.querySelector('.myClass').innerHTML;
document.querySelector('.myClass').innerText;
Use the data-* attribute to embed custom data. eg:
<div class='myClass' data-price="123"> Your ticket costs <i> 123 </i> </div>
Then you can use
document.querySelector('.myClass').getAttribute('data-price');

You need to use .innerText to get the numbers in the strong tag
document.querySelector('.myClass strong').innerText;

Related

Get element by data-i18n-key [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 days ago.
I'm trying to get an element which is added by an external application. The only way I can get this specific element is by the data-i18n-key attribute which I thought I can grab like any data attribute so something like this.
The code:
const buttons = document.querySelectorAll('[data-i18n-key="sdk.seamless_product_reward.paid_items_required"]');
console.log(buttons.length);
<span class="lion-reward-item__redeem-button-text lion-loyalty-page-reward-item__redeem-button-text" data-i18n-key="sdk.seamless_product_reward.paid_items_required">Paid items required</span>
However, this doesn't return anything. Any ideas how to do this?
Of course, Barmer is absolutely right. Your code works. The problem will be that your JS is initialised before the DOM has finished loading. Pack your JS above the closing body tag.

id value as variable vs document.getElementById [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
document.getElementById("someId") Vs. someId
(5 answers)
Closed 4 years ago.
I have a snippet of code here
<div id="pTagId" >some content</div>
<button onclick="console.log(document.getElementById('pTagId').innerHTML);">button1</button>
<button onclick="console.log(pTagId.innerHTML);">button2</button>
In the first button I'm using document.getElementById to get the tag object.
In the second button I'm directly using pTagId without document.getElementById and it also works.
is it reliable to use pTagId directly without document.getElementById? If yes what is the need of document.getElementById?
getElementById() is the right way to do it.
There are some browsers that make a global variable by the same name as the element id so that may be why it was somehow working, but you should not rely on that.

How to get the `html` element in Javascript? [duplicate]

This question already has answers here:
How to get the <html> tag HTML with JavaScript / jQuery?
(6 answers)
Closed 4 years ago.
I want to get the html element in Javascript. I can now use document.body.parentNode or document.getElementsByTagName("html"). But I think there should be a simple, "right" way to do it.
So, what's the right way to get the html element in Javascript?
I tried to use search engines, but I don't know how to tell them the html tag is different from any other HTML tag, so it yielded no result I wanted.
You can use:
document.documentElement
which points to the document's root html node.
https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
Chek this answer
https://stackoverflow.com/a/22873490/3134112
reference:
https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.
I'd say document.getElementsByTagName, querySelector etc. are all "right ways" to get reference to html tag, it doesn't get simpler than that.

How may I know the number of same elements in Javascript? [duplicate]

This question already has answers here:
How can I count the number of elements with same class?
(6 answers)
Closed 5 years ago.
I've got the following html :
<div class="same"></div>
<div class="same"></div>
And I want to print the number of div with the id "same" like this
<p><script>document.write(getElementbyId("#same").sum);</script></p>
And it would print : 2...
It actually doesn't work, I know I don't use the good Javascript function.
You'd use .length and .getElementsByClassName() like this:
<div class="same"></div>
<div class="same"></div>
<p><script>document.write(document.getElementsByClassName("same").length);</script></p>

Selecting an element whose ID has a period in it [duplicate]

This question already has answers here:
Can't select div with id=":1"
(2 answers)
Closed 7 years ago.
I have an element that has a period in it:
<div id="parent">
<div id="my.element"></div>
</div>
and I want to select it using Prototype. I've tried this:
$$('#parent #my.element');
but it isn't working because it thinks the .element part is a class. Is there a way around this?
FYI, it isn't really an option to rename the element. Unfortunately I'm stuck with this naming scheme as well as only Prototype
Thanks
You can always escape your dot in css, so this should work
$$('#parent #my\\.element');
EDIT: As stated by Oriol and tested, you indeed have to use 2 slashes

Categories