-2

I have

    <nav>
        <ul class="sel">
              <li>
                  <a href="#LINK1" data-title="A">A</a>
              </li>
              <li>
                  <a href="#LINK2"  data-title="B">B</a>
              </li>
              <li>
                  <a href="#LINK3" data-title="C">C</a>
            </li>
        </ul>
    </nav>
</header>

When the page loads I would like the <li> with <a href='#LINK1' data-title="A">A</a> to be selected and showing. I have tried multiple jQuery solutions and none have worked.

Is it possible to have this <li> containing this specific <a> show when the page loads?

I have many solutions such as:

Using jQuery to 'click' a li element

Click trigger on page load (UL > LI)

Community
  • 1
  • 1
Mike El Jackson
  • 771
  • 3
  • 14
  • 23

2 Answers2

1

Your HTML is invalid; the href attribute defines a target url for the <a> element to navigate to, but you were using it to define your id. Instead, the id should be applied to the <li> element you are trying to hide/show. Take a look at the example below, which hides the first <li> element if the other elements are clicked:

var display = function(pageHide,pageShow) {
  var current = "#DATA" + pageShow.toString();
  $(current).show();
  var last = "#DATA" + pageHide.toString();
  $(last).hide();
}; // hide/show pages.

$(document).ready(function() {
  var page = 1;         // set default page
  $('#DATA2').hide();   // hide others
  $('#DATA3').hide();
  $('#LINK1').click(function() {
    display(page,1);
    page = 1;
  });
  $('#LINK2').click(function() {
    display(page,2);
    page = 2;
  });
  $('#LINK3').click(function() {
    display(page,3);
    page = 3;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <ul class="sel">
    <li id="LINK1">
      <a data-title="A" href="#">A</a>
    </li>
    <li id="LINK2">
      <a data-title="B" href="#">B</a>
    </li>
    <li id="LINK3">
      <a data-title="C" href="#">C</a>
    </li>
  </ul>
  <p id="DATA1">main</p>
  <p id="DATA2">graphs</p>
  <p id="DATA3">notes</p>
</nav>
Malcolm McSwain
  • 1,888
  • 2
  • 15
  • 17
  • @MikeElJackson Please take a look at the edited answer. Does this answer your question? – Malcolm McSwain Mar 02 '17 at 03:22
  • so A, B, and C are tabs on a dashboard. A corresponds to a main page, B corresponds to graphs, and C corresponds to a notes section. When the web page is loaded I want A, the main page, to show. But when I click the tab for B or C I want it to switch to the respective graphs or notes section. However, when I click the A tab again I want the main page to show. I have the tabs switching between each other but I can't get the A tab to show when the page loads. The code you have above removes A completely when BorC is selected, but I am trying to select A and then switch to B,C and then back to A – Mike El Jackson Mar 02 '17 at 05:45
  • right now when I load the webpage neither A nor B nor C show – Mike El Jackson Mar 02 '17 at 05:46
  • @MikeElJackson Ok, I get a basic sense of what you are trying to accomplish... take a look at the new code snippet. There is only one disadvantage to using this method, which is that if you press a link twice it will hide the current page, but it's a start. Is this what you were looking for? – Malcolm McSwain Mar 02 '17 at 17:34