How do I do this? Please help if you could,
<div id="user"> one </div>
<div id="user"> two </div>
<script>
document.getElementById('user').innerHTML = "three";
</script>
I was looking if we can replace all the divs with same ID with "three"
Although it's highly discouraged and invalid to use identical IDs in HTML, there is a way to do it in most browsers:
var elements = document.querySelectorAll('[id="user"]');
for(var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "three";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With