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>
Related
This question already has answers here:
Create <div> and append <div> dynamically
(9 answers)
Closed 2 years ago.
I used a query selector to get the div i want to append. But the created div doesn't want to append to anything other than the body. Is there a way i can make it specifically append under the div?
Look at this: https://www.w3schools.com/jsref/met_node_appendchild.asp
What you need to do is
parent.appendChild(thatDiv);
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;
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.
This question already has answers here:
Set attribute without value
(7 answers)
Closed 4 years ago.
I want to add an attribute on a div, something like this:
<div class='media' data-type-sticky></div>
Using jQuery, so i've tried this:
jQuery('.media').attr(data-type-sticky);
jQuery('.media').attr('data-type-sticky');
Just pass empty string to attr value.
$('.media').attr('data-type-sticky','');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='media'>Hello</div>
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