Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:25:58+00:00 2026-05-24T00:25:58+00:00

I have a Javascript as follows; if (document.getElementsByClassName(‘someClass’)) { obj = document.getElementsByClassName(‘someClass’); } else

  • 0

I have a Javascript as follows;

if (document.getElementsByClassName('someClass'))
{
obj = document.getElementsByClassName('someClass');
}
else if (document.getElementById('someId'))
{
obj = document.getElementById('someId');
}

Now there is a for loop which acts on this “obj”

for(i=0; i<obj.length;i++){
obj[i].addEventListener() // Pseudo code shown here
} 

The issue is this works fine if obj is returned as an array i.e. from document.getElementsByClassName.

But if document.getElementById('someId') is true, it does not return an array and the for loop fails to execute.

How can I fix this issue, given that I cannot do anything to the HTML code itself?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:25:58+00:00Added an answer on May 24, 2026 at 12:25 am

    You can simply create an array:

    else if (document.getElementById('someId')) {
        obj = [document.getElementById('someId')]; // note the array literal
    }
    

    The return value of getElementById will always be a DOM element, so you cannot change that. IDs are supposed to be unique, so even if you have several elements with the same ID, it will return only one of them.

    Given that getElementsByClassName does not exist in IE8 and below, you should also have a look at document.querySelectorAll [docs] (which at least works in IE8).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript code coupon.js defined as follows - jQuery(document).ready(function(){ jQuery('.appnitro').submit( function() {
I have a jQuery Dialog which initializes hotkeys as follows: <script type=text/javascript> $(document).bind('keydown', '<%=(i+1)%>',function
In javascript I have an array as follows: var foo = [2, 2, 4,
Is there anyway to have javascript run when a XSL sheet has been applied
I have placed following javascript in my html file. <script TYPE=text/javascript> function srk(){ document.ontouchmove
I have the worlds most simple javascript function: fnSubmit() { window.print(); document.formname.submit(); } Which
I have been reading the JavaScript Scripting Guide for Quicktime and in there it
I'm having a problem with getElementbyId as follows: <script type=text/javascript> <!-- function show_links(locale) {
I have a JavaScript function that is not working now that I put in
I have the following javascript function in my extension: function findPos(obj) { var curleft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.