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

  • Home
  • SEARCH
  • 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 6171935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:18:22+00:00 2026-05-23T23:18:22+00:00

If i have something like this : function foo(class){ for(i=0; i<(class.length);i++){ return document.getElementsByClassName(class)[i]; }

  • 0

If i have something like this :

function foo(class){
    for(i=0; i<(class.length);i++){
       return document.getElementsByClassName(class)[i];
    }
}

And now i want to do something like this:

 foo("someclass").innerHTML="something";

it will do it only for the first element and i understand why this happens and how to make it work correctly, but how can i make the function get other methods without telling it in the loop exactly what to do,like this

   foo("someclass").innerHTML="something";//this to put something inside the element
   foo("someclass").style.backgroundColor="#000");// and this to work also

So,if its possible,how can i make the function do this without putting these methods in the foo function loop? Is there a way to put these methods in a variable like this

  function foo(class).variableMethod{
for(i=0; i<(class.length);i++){
   document.getElementsByClassName(class)[i].variableMethod;
}

}

Is this possible?

  • 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-23T23:18:22+00:00Added an answer on May 23, 2026 at 11:18 pm

    You can pass a function to foo(), and have foo() call that function with each matched element:

    function foo(className, func)
    {
        var elements = document.getElementsByClassName(className);
        for (var i = 0; i < elements.length; ++i) {
            func(elements[i]);
        }
    }
    

    Now you can do something like:

    foo("someclass", function(element) {
        element.innerHTML = "something";
        element.style.backgroundColor = "#000";
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript class like this: function Foo() { this.x = 5; this.y
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do
So I have a function that looks something like this: float function(){ float x
I have a function that looks something like this: //iteration over scales foreach ($surveyScales
This is a C# console application. I have a function that does something like
I have something like this: <div class=content> <a href=#>A</a> </div> <div class=content> <a href=#>B</a>
Ok, I have a class like this: public class Foo extends Sprite { public
I have a C++ class which presents a fluent interface, something like: class Foo
I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
I have something like this: <node TEXT= txt A /> <node TEXT= txt X

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.