Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Javascript Expandable Rows ... First row expanded ... need it closed on page load

I have an expandable table that I modified... Right now it functions very well, but the FIRST ROW is expanded on page load. And I need that first row to be closed. Javascript is below.

JavaScript:
$(document).ready(function () {
  const expchartcontents = $('.expchart');

  expchartcontents.each((index, expchartcontents) => {
  let selecteditem = 0;

  let expchartItemIndex = -1;
  const changeTab = (index, item) => {
      var delay = 300;
      if (window.innerWidth < 980) {
        delay = 800;
      }
      if (expchartItemIndex !== index) {
            $(expchartcontents).find(".expchart-row")
                .eq(expchartItemIndex)
                .children(".expchartpane-subitem")
                .slideUp(delay);
            $(expchartcontents).find(".expchart-row")
                .eq(expchartItemIndex)
                .removeClass("clicked");
          
            $(expchartcontents).find(item)
                .children(".expchartpane-subitem")
                .slideDown(delay);
            $(expchartcontents).find(item)
                .addClass("clicked");
            expchartItemIndex = index;

      } else {
          $(expchartcontents).find(".expchart-row")
            .eq(expchartItemIndex)
            .children(".expchartpane-subitem")
            .slideUp(delay);
          $(expchartcontents).find(".expchart-row")
            .eq(expchartItemIndex)
            .removeClass("clicked");
          expchartItemIndex = -1;
      }
};
/* Click closes others */
$(expchartcontents).find(".expchart-row").each((index, item) => {
      $(expchartcontents).find(item).click(() => {
            changeTab(index, item);
            selecteditem = index;
      });
});
changeTab(0, $(expchartcontents).find(".expchart-row").eq(0));
  });
});

I don't know javascript very well, so I've been trying various things. If I change the last line ... it changes some of the opening states, but the 1st is still open.

JavaScript:
changeTab(-1, $(expchartcontents).find(".expchart-row").eq(-1));
/^ does nothing (first row still expanded)  */
changeTab(0, $(expchartcontents).find(".expchart-row").eq(0));
/^ does nothing (first row still expanded) */
changeTab(1, $(expchartcontents).find(".expchart-row").eq(1));
/^ opens 2nd row, in addition to the 1st */
changeTab(2, $(expchartcontents).find(".expchart-row").eq(2));
/^ opens 3rd row, in addition to the 1st */

I've also tried changing the initial variables, hoping they'd change, but that didn't help either...

JavaScript:
let selecteditem = 0; /* tried -1, 1, 2, 3 ... no change */
let expchartItemIndex = -1; /* tried 0, 1, 2, 3 ... no change */

Here is a codepen for easier viewing...

https://codepen.io/jabbamonkey/pen/QWXqWVM
 
I have an expandable table that I modified... Right now it functions very well, but the FIRST ROW is expanded on page load. And I need that first row to be closed. Javascript is below.

JavaScript:
$(document).ready(function () {
  const expchartcontents = $('.expchart');

  expchartcontents.each((index, expchartcontents) => {
  let selecteditem = 0;

  let expchartItemIndex = -1;
  const changeTab = (index, item) => {
      var delay = 300;
      if (window.innerWidth < 980) {
        delay = 800;
      }
      if (expchartItemIndex !== index) {
            $(expchartcontents).find(".expchart-row")
                .eq(expchartItemIndex)
                .children(".expchartpane-subitem")
                .slideUp(delay);
            $(expchartcontents).find(".expchart-row")
                .eq(expchartItemIndex)
                .removeClass("clicked");
         
            $(expchartcontents).find(item)
                .children(".expchartpane-subitem")
                .slideDown(delay);
            $(expchartcontents).find(item)
                .addClass("clicked");
            expchartItemIndex = index;

      } else {
          $(expchartcontents).find(".expchart-row")
            .eq(expchartItemIndex)
            .children(".expchartpane-subitem")
            .slideUp(delay);
          $(expchartcontents).find(".expchart-row")
            .eq(expchartItemIndex)
            .removeClass("clicked");
          expchartItemIndex = -1;
      }
};
/* Click closes others */
$(expchartcontents).find(".expchart-row").each((index, item) => {
      $(expchartcontents).find(item).click(() => {
            changeTab(index, item);
            selecteditem = index;
      });
});
changeTab(0, $(expchartcontents).find(".expchart-row").eq(0));
  });
});

I don't know javascript very well, so I've been trying various things. If I change the last line ... it changes some of the opening states, but the 1st is still open.

JavaScript:
changeTab(-1, $(expchartcontents).find(".expchart-row").eq(-1));
/^ does nothing (first row still expanded)  */
changeTab(0, $(expchartcontents).find(".expchart-row").eq(0));
/^ does nothing (first row still expanded) */
changeTab(1, $(expchartcontents).find(".expchart-row").eq(1));
/^ opens 2nd row, in addition to the 1st */
changeTab(2, $(expchartcontents).find(".expchart-row").eq(2));
/^ opens 3rd row, in addition to the 1st */

I've also tried changing the initial variables, hoping they'd change, but that didn't help either...

JavaScript:
let selecteditem = 0; /* tried -1, 1, 2, 3 ... no change */
let expchartItemIndex = -1; /* tried 0, 1, 2, 3 ... no change */

Here is a codepen for easier viewing...

https://codepen.io/jabbamonkey/pen/QWXqWVM


To ensure that the first row of your expandable table is not expanded on page load, you'll need to remove or modify the line that expands the first row when the page loads. Specifically, the changeTab(0, $(expchartcontents).find(".expchart-row").eq(0)); line is responsible for this initial expansion.

Here’s how you can adjust your code:

  1. Remove the Initial Expansion:Remove the changeTab(0, $(expchartcontents).find(".expchart-row").eq(0)); line so that no rows are expanded when the page first loads.
  2. Ensure the Rows are Collapsed Initially:Optionally, you can make sure that all rows are collapsed by default on page load, though they should already be in that state without the above line.
Here’s your modified JavaScript:

$(document).ready(function () {
const expchartcontents = $('.expchart');

expchartcontents.each((index, expchartcontents) => {
let selecteditem = 0;
let expchartItemIndex = -1;

const changeTab = (index, item) => {
var delay = 300;
if (window.innerWidth < 980) {
delay = 800;
}
if (expchartItemIndex !== index) {
$(expchartcontents).find(".expchart-row")
.eq(expchartItemIndex)
.children(".expchartpane-subitem")
.slideUp(delay);
$(expchartcontents).find(".expchart-row")
.eq(expchartItemIndex)
.removeClass("clicked");

$(expchartcontents).find(item)
.children(".expchartpane-subitem")
.slideDown(delay);
$(expchartcontents).find(item)
.addClass("clicked");
expchartItemIndex = index;
} else {
$(expchartcontents).find(".expchart-row")
.eq(expchartItemIndex)
.children(".expchartpane-subitem")
.slideUp(delay);
$(expchartcontents).find(".expchart-row")
.eq(expchartItemIndex)
.removeClass("clicked");
expchartItemIndex = -1;
}
};

/* Click closes others */
$(expchartcontents).find(".expchart-row").each((index, item) => {
$(expchartcontents).find(item).click(() => {
changeTab(index, item);
selecteditem = index;
});
});

// The following line is removed to ensure no row is expanded on load
// changeTab(0, $(expchartcontents).find(".expchart-row").eq(0));
});
});


Explanation:​

  • changeTab(0, $(expchartcontents).find(".expchart-row").eq(0));:
    • This line was responsible for expanding the first row when the page loaded. By removing it, you prevent any row from being expanded initially.
Now, when the page loads, all rows will remain collapsed until the user clicks on one to expand it.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom