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 561113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:24:56+00:00 2026-05-13T12:24:56+00:00

I am trying to get some elems with the classname special. I found the

  • 0

I am trying to get some elems with the classname “special”.
I found the following script online, but it only returns an empty array.

Does anyone see what’s wrong?

getElementsByClassName = function (node, classname){
 var a = [],
    re = new RegExp('\b' + classname + '\b'),
    els = node.getElementsByTagName("*"),
    l = els.length,
    i;

 for (i = 0; i < l; i += 1) {
  if (re.test(els[i].className)) {
      a.push(els[i]);
  }
 }
 console.log(a)
 return a;
}

var wrap = document.getElementById('wrap');
getElementsByClassName(wrap, 'special')

wrap contains 22 children, the last one is <p class="special">Lorem</p>, and in firebug I get all the way down to finding the node with the classname, but then it jumps the a.push. Im lost!

edit:
okay so it does work now, it would still be interesting though to know why console.log(a) return an empty array

  • 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-13T12:24:56+00:00Added an answer on May 13, 2026 at 12:24 pm

    re = new RegExp(‘\b’ + classname + ‘\b’),

    \b in a string literal is a backspace character. They meant:

    var re= new RegExp('\\b' + classname + '\\b');
    

    However this is still wrong, because:

    1. it won’t work for any classnames that contain non-ASCII or non-alphanumeric characters, as that would put the word boundaries in the wrong place;

    2. classnames may contain characters that have special meaning in regex, such as .; these would need to be escaped.

    You can find an alternative implementation that should match the standard document.getElementsByClassName interface better in this question.

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

Sidebar

Related Questions

Just trying to get some rows out of a database and loop through but
Im trying to get some system information using WMI but the problem is when
I am trying to get some XML data with LINQ, but running into a
I followed some online guides trying to get some headless VMs to start/suspend automatically
I am trying to get some variables into a script I am loading. Currently,
I'm trying to get some json from an action but I have a problem.
I'm trying get some licensing code from AndroidPit.com working, but I get Unable to
Im trying to get some simple javascript working in gwt but keep failing. The
Trying to get some movies and their genres but leave out any records that
Well, I'm trying get some values from places.sqlite database. But when I do $

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.