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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:21:03+00:00 2026-06-10T00:21:03+00:00

I have a function whose destination is to work onClick event. So, we have

  • 0

I have a function whose destination is to work onClick event.

So, we have for example 4 Span elements and 4 Div elements.
The Spans are Tabs-buttons which I would like to “open” those Divs.

The 1st Span onClick would (open) change the style.display of the 1st Div in “block”, from “none”, and so on for the next Spans.

This piece of code works very well, but it changes only the design of elements.

function activateSup(s) {
  var workTable = s.parentNode.parentNode.parentNode.parentNode.parentNode;
  var spans = workTable.getElementsByTagName("span");
  var supDivs = workTable.getElementsByClassName("supDiv");

  for (var i = 0; i < spans.length; i++) {
    spans[i].style.backgroundColor = "";
    spans[i].style.border = "";
  }

  s.style.backgroundColor = "#5eac58";
  s.style.border = "2px solid #336633";
}

I’ve tried to add the code below into my function to achieve what I want, but It does not work.

  var getIndex = function(s) {
    for (var index = 0; s != s.parentNode.childNodes[index]; index++);
    return index;
  }
  for (var d = 0; d < supDivs.length; d++) {
    if (getIndex == d) {
        supDivs[d].style.display = "block";
    }
    else {
        supDivs[d].style.display = "none";
    }
  }
  • 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-10T00:21:04+00:00Added an answer on June 10, 2026 at 12:21 am

    I’m not exactly sure what you’re trying to do, but one thing I noticed is this:

    var getIndex = function(s) { /* .... */ }
    
    for (var d = 0; d < supDivs.length; d++) {
        if (getIndex == d) {
            supDivs[d].style.display = "block";
        }
        else { /* ... */ }
    }
    

    This code is comparing getIndex to d, which means it’s comparing an integer (d) to the function getIndex, instead of the result of the function call getIndex(spans[d]) (which is an integer, like d).

    But what I think you’re really trying to do, is getting the index of the clicked <span> so you can show the <div> with the matching index (and hide the rest). To achieve this, the code could be changed like so:

    function activateSup(s) {
        var workTable = s.parentNode.parentNode.parentNode.parentNode.parentNode;
        var spans = workTable.getElementsByTagName("span");
        var supDivs = workTable.getElementsByClassName("supDiv");
        var index;    
    
        for (var i = 0; i < spans.length; i++) {
            spans[i].style.backgroundColor = "";
            spans[i].style.border = "";
    
            if (s == spans[i])
                index = i;                    
        }
    
        s.style.backgroundColor = "#5eac58";
        s.style.border = "2px solid #336633";
    
        for (var d = 0; d < supDivs.length; d++) {
            if (index == d) {
                supDivs[d].style.display = "block";
            } else {
                supDivs[d].style.display = "none";
            }
        }
    }
    

    Instead of the function getIndex, this just saves the correct index inside the first for loop.

    There are many more improvements that could be made to this code, like rewriting it so you don’t need that ugly s.parentNode.parentNode.parentNode.parentNode.parentNode and working with CSS classes instead of manually setting the style. But I’ll leave that to the reader.

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

Sidebar

Related Questions

i have build a small function whose work to get value in alert box
I have a function whose return type is Table This code does not work.
i have build a small function whose work is to change banner image path
I have a function whose input argument can either be an element or a
I have a function in my namespace, ns::foo , whose job is to dispatch
I have an activity in whose onCreate method an Init function is called (the
function abc() { document.form1.1.disabled=true; } I have a select box whose id is 1
I have a function whose prototype is as follows: void foo(const char * data);
I have a function whose purpose is to list the folders within the images
I have created one function whose purpose is to add data to database (I'm

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.