Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
getElementsByClassName not showing data in an element

I know there are a lot of questions regarding the getElementsByClassName not working, however I browsed through many and coudldnt find an answer for my situation

Basically I have the following code

<script>

var res = localStorage.getItem('img');
    if(res == null){
            const myList = ['๐Ÿ’', '๐Ÿ•', '๐Ÿˆ', '๐Ÿ…', '๐ŸŽ', '๐Ÿฆ“', '๐Ÿ„', '๐Ÿฆ’', '๐Ÿ˜', '๐Ÿฆ”', '๐Ÿฆ‰', '๐ŸŠ', '๐Ÿฆ–', '๐Ÿฌ', '๐Ÿฆˆ', '๐Ÿฆ‹', '๐Ÿž', '๐Ÿฆ‚'];
            res  = myList[Math.floor(Math.random() * myList.length)];
            localStorage.setItem('img', res);
        }
console.log(res);

document.getElementsByClassName("emoji").innerHTML = res

And several spans with same class

<span class="emoji" style="font-size: 40px !important;"></span>

The problem is that the "res" doesn't print anything in span

The console log prints everything fine and LS stores the information perfectly

I have tried with ID's

document.getElementsById("emoji").innerHTML = res

And it works perfectly, however only with the first div (as it should i suppose)

What could i be doing wrong in this situation? Maybe im blind and dont see a very simple mistake in my code

Thanks in advance

about 4 years ago ยท Juan Pablo Isaza
3 answers
Answer question

0

Multiple elements can have the same class name. getElementsByClassName returns an array of elements that have the class name (it's Elements not Element). So if you have only 1 element with that class name, you can do document.getElementsByClassName("emoji")[0].innerHTML = res.

about 4 years ago ยท Juan Pablo Isaza Report

0

If you specify classname so you have to give index to the class as it puts response only in the first element in the case of id, it is unique but in case of class you have to specify.

document.getElementsByClassName("emoji")[0].innerHTML = res;
about 4 years ago ยท Juan Pablo Isaza Report

0

getElementByClassName always return array of element, usually can read an element from array as index basis, also similar way can get the element from getElementByClassName.Pls see the below snippet.

const myList = ['๐Ÿ’', '๐Ÿ•', '๐Ÿˆ', '๐Ÿ…', '๐ŸŽ', '๐Ÿฆ“', '๐Ÿ„', '๐Ÿฆ’', '๐Ÿ˜', '๐Ÿฆ”', '๐Ÿฆ‰', '๐ŸŠ', '๐Ÿฆ–', '๐Ÿฌ', '๐Ÿฆˆ', '๐Ÿฆ‹', '๐Ÿž', '๐Ÿฆ‚'];
var res  = myList[Math.floor(Math.random() * myList.length)];
document.getElementsByClassName("emoji")[0].innerHTML = res
<span class="emoji" style="font-size: 40px !important;"></span>

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!