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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:17:36+00:00 2026-06-17T15:17:36+00:00

This is from a section of a much more complicated script. In short, I’m

  • 0

This is from a section of a much more complicated script. In short, I’m trying to do 2 things here:

  1. Get an element’s children’s class names, and remove the word “span” from them, then add them to a variable (this works fine).
  2. Get those class names (in full, with ‘span’ still attached) and add them to an array.

Here’s my code:

var currentSpans = [];

$('container').children().each(function(i2, spans) {
    if (spans.className.indexOf('span') != -1) {
        total += parseInt(spans.className.replace(/[A-Za-z$-]/g, ""),10);
        currentSpans.push(spans.attr('class'));
    }

});

It breaks at currentSpans.push(spans.attr('class')); and says #<HTMLDivElement> has no method 'attr'

        currentSpans.push(spans.className());

as it was used above, but here it doesn’t work (this was expected though).

What am I doing wrong here? It seems like a simple enough problem but I’m coming up blank, might be because I’ve been working with jquery all day!

  • 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-06-17T15:17:37+00:00Added an answer on June 17, 2026 at 3:17 pm

    spans.attr('class') should be $(spans).attr('class').

    When you are accessing spans.className you are using the dom object reference, attr() is not a method of the dom object but a method provided by jQuery so you need to use the jQuery reference to that element by using $(spans).

    Updated:

    $('.row-fluid').each(function (i, ele) {
        var total = 0;
        var currentSpans = [];
    
        $(ele).children().each(function (i2, spans) {
            var className = $(spans).attr('class');
            var spanClass = /span\d+/.exec(className);
            spanClass = spanClass.length > 0 ? spanClass[0] : undefined;
            if (spanClass) {
                total += parseInt(spanClass.replace("span", ""), 10);
                currentSpans.push(spanClass);
            }
    
        });
    
        console.log(currentSpans, total);
    });
    

    Fiddle:

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

Sidebar

Related Questions

I'm cross-posting this from the wordpress section, b/c I think the problem lies more
This is the code section from inno setup.My intention is to make two Checkbox
This is what I have typed in the footer message section from the site
I've been trying this from a long time. I'm having today's date and I
This is probably a noob question that I will get slated for but here
This is a section from Dive Into Python 3 regarding strings: In Python 3,
I'm trying to parse some JSON data, and I'm using the examples from this
The following code is taken from much larger code but I believe that this
I have this route: {controller}/{id}/{action} Because I think it makes more sense from RESTful
This may be a simple oversight on my part (or something much more advanced

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.