2

I am trying to implement a navigation sidebar on left with list of links(<a>) such that on click of each links, corresponding <div> will be loaded on right side.

When I have a function for onclick property in <a>. It gives me error on browser console.

Uncaught ReferenceError: callFunction is not defined

Here is my implementation:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 function callFunction(){alert("Load divs");}
});
</script>

<style>
div.container {
    width: 100%;
    border: 1px solid gray;
}

header, footer {
    padding: 1em;
    color: white;
    background-color: powderblue;
    clear: left;
    text-align: center;
}

nav {
    float: left;
    max-width: 160px;
    margin: 0;
    padding: 1em;
}

nav ul {
    list-style-type: none;
    padding: 0;
}
   
nav ul a {
    text-decoration: none;
}

article {
    margin-left: 170px;
    border-left: 1px solid gray;
    padding: 1em;
    overflow: hidden;
}
</style>
</head>
<body>

<div class="container">

<header>
   <h1>My Company</h1>
</header>
  
<nav>
  <ul>
    <li><a href="#" onclick="callFunction()">Link1</a></li>
    <li><a href="#" onclick="callFunction()">Link2</a></li>
    <li><a href="#" onclick="callFunction()">Link3</a></li>
  </ul>
</nav>

<!--<div id="Link1" class="class1">
 <h1>Link1</h1>
 <p>Link1 detail</p>
</div>

<div id="Link2" class="class1">
 <h1>Link2</h1>
 <p>Link2 detail</p>
</div>-->

<footer>Copyright &copy; myCompany</footer>

</div>

</body>
</html>

My requirement is to have a list of Links and s, where s should be hidden, and only be appeared when corresponding Links are clicked.

Om Sao
  • 7,064
  • 2
  • 47
  • 61
  • You are getting error because `callFunction()` you have defined inside `$(document).ready();` which means, when document is ready then only `callFunction()` function is loaded, by then already error is thrown. therefore, define the function outside `$(document).ready();` – Om Sao Aug 26 '17 at 09:34
  • Possible duplicate of [onclick for button not calling function](https://stackoverflow.com/questions/45867078/onclick-for-button-not-calling-function) – kukkuz Aug 26 '17 at 09:41
  • 1
    @kukkuz: I understood my mistake now, now I think i got answer for navigation side bar implementaion also. ! –  Aug 26 '17 at 09:43

2 Answers2

2

Here is the code that should work for you.

  1. <div>s are not rendered/shown intially.

  2. On click of Links(<a>), corresponding <div>s are shown

  3. Navigation side bar is maintained.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">

 function loadContent(selector){
  $("#loadOnClick").html($(selector).html());
 };

 
 $(document).ready(function(){
    
  loadContent("#userGuide");
    
    });
</script>
<style>
div.container11 {
    width: 100%;
    
}
section.container1{
 display: -webkit-flex; /* Safari */
 display: flex;
}

.displayInline{
 -webkit-flex: 1;  /* Safari 6.1+ */
    -ms-flex: 1;  /* IE 10 */    
    flex: auto;
}

header, footer {
    padding: 1em;
    color: white;
    background-color: powderblue;
    clear: left;
    text-align: center;
}

nav {
 border-right: 2px solid gray;
}

nav ul {
    list-style-type: none;
    padding-top: 5px;
}
   
nav ul a {
    text-decoration: none;
 line-height: 30px;
}

div#loadOnClick {
 float: right;
}

 .displayOnClick{
 display: none;
 }
</style>
</head>
<body>

<div class="container">

<header>
   <h1>My Company</h1>
</header>

 <section id="container1">
 <nav class="displayInLine" style="width: 20%; float: left;">
   <ul>
  <li><a href="#userGuide" class="quickLinks" onclick='loadContent("#userGuide")'>User Guide</a></li>
  <li><a href="#SOP" class="quickLinks" onclick='loadContent("#SOP")'>SOP</a></li>
  <li><a href="#procedurePages" class="quickLinks" onclick='loadContent("#procedurePages")'>Procedure pages</a></li>
  <li><a href="#departmentInformation" class="quickLinks" onclick='loadContent("#departmentInformation")'>Department information</a></li>
  <li><a href="#hoursOfOperations" class="quickLinks" onclick='loadContent("#hoursOfOperations")'>Hours of operations</a></li>
   </ul>
 </nav>

 <div class="displayInLine" id="loadOnClick" style="width:75%; float: right;">
  
 </div>
</section>

<div id="userGuide" class="displayOnClick">
 <h1>User Guide</h1>
 <p>This is the userguide for employees</p>
</div>

<div id="SOP" class="displayOnClick">
 <h1>SOP</h1>
 <p>This is the Statement of purpose for employees</p>
</div>

<div id="procedurePages" class="displayOnClick">
 <h1>Procedure pages</h1>
 <p>This is the Procedure pages for employees</p>
</div>

<div id="departmentInformation" class="displayOnClick">
 <h1>Department information</h1>
 <p>This is the Department information for employees</p>
</div>

<div id="hoursOfOperations" class="displayOnClick">
 <h1>Hours of operations</h1>
 <p>This is the Hours of operations for employees</p>
</div>

<footer>Copyright &copy; Om Sao</footer>

</div>

</body>
</html>
Om Sao
  • 7,064
  • 2
  • 47
  • 61
0

You dont need to call callFunction() on $(document).ready state because thats called only once the page is loaded and by the time document is ready/loaded, the error is thrown. Hence, put definition outside $(document).ready.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function callFunction(){alert("Load divs");}
</script>

<style>
div.container {
    width: 100%;
    border: 1px solid gray;
}

header, footer {
    padding: 1em;
    color: white;
    background-color: powderblue;
    clear: left;
    text-align: center;
}

nav {
    float: left;
    max-width: 160px;
    margin: 0;
    padding: 1em;
}

nav ul {
    list-style-type: none;
    padding: 0;
}
   
nav ul a {
    text-decoration: none;
}

article {
    margin-left: 170px;
    border-left: 1px solid gray;
    padding: 1em;
    overflow: hidden;
}
</style>
</head>
<body>

<div class="container">

<header>
   <h1>My Company</h1>
</header>
  
<nav>
  <ul>
    <li><a href="#" onclick="callFunction()">Link1</a></li>
    <li><a href="#" onclick="callFunction()">Link2</a></li>
    <li><a href="#" onclick="callFunction()">Link3</a></li>
  </ul>
</nav>

<!--<div id="Link1" class="class1">
 <h1>Link1</h1>
 <p>Link1 detail</p>
</div>

<div id="Link2" class="class1">
 <h1>Link2</h1>
 <p>Link2 detail</p>
</div>-->

<footer>Copyright &copy; myCompany</footer>

</div>

</body>
</html>
Om Sao
  • 7,064
  • 2
  • 47
  • 61
k13k
  • 49
  • 4