Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

JS tabs error need help

Hey all,

Im having trouble with this script, which is showing and hiding tabs. I have an error on this line:

tabs[i].onclick=Site.displayPage();

Open in new window



var Site = {
	
			start: function(){
				console.log('start');
				Site.tabsShow();				
			},
			
			tabsShow: function(){	
			
			
			
			console.log('displayPage');
			
			
			var container = document.getElementById("tabContainer");
    			// set current tab
    			var navitem = container.querySelector(".tabs ul li");
    			//store which tab we are on
    			var ident = navitem.id.split("_")[1];
    			navitem.parentNode.setAttribute("data-current",ident);
    			//set current tab with class of activetabheader
    			navitem.setAttribute("class","tabActiveHeader");
				
    			//hide two tab contents we don't need
    			var pages = container.querySelectorAll(".tabpage");
    			for (var i = 1; i < pages.length; i++) {
    			  pages[i].style.display="none";
    			}
				
    			//this adds click event to tabs
    			var tabs = container.querySelectorAll(".tabs ul li");
    			for (var i = 0; i < tabs.length; i++) {
    			  tabs[i].onclick=Site.displayPage();				
    			}
	
			},
			
			displayPage: function() {
			var current = this.parentNode.getAttribute("data-current");
			  //remove class of activetabheader and hide old contents
			  document.getElementById("tabHeader_" + current).removeAttribute("class");
			  document.getElementById("tabpage_" + current).style.display="none";
			
			  var ident = this.id.split("_")[1];
			  //add class of activetabheader to new active tab and show contents
			  this.setAttribute("class","tabActiveHeader");
			  document.getElementById("tabpage_" + ident).style.display="block";
			  this.parentNode.setAttribute("data-current",ident);
			}
		
		
			
		};
		window.addEvent('domready', Site.start);

Open in new window



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pure Javascript, HTML 5 &amp; CSS3 Tabs</title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/mootools.js"></script>

</head>
<body>
<div id="wrapper">
  <div id="tabContainer">
    <div class="tabs">
      <ul>
        <li id="tabHeader_1">Page 1</li>
        <li id="tabHeader_2">Page 2</li>
        <li id="tabHeader_3">Page 3</li>
      </ul>
    </div>
    <div class="tabscontent">
      <div class="tabpage" id="tabpage_1">
        <h2>Page 1</h2>
        <p>Pellentesque habitant morbi tristique senectus...</p>
      </div>
      <div class="tabpage" id="tabpage_2">
        <h2>Page 2</h2>
        <p>Pellentesque habitant morbi tristique senectus...</p>
      </div>
      <div class="tabpage" id="tabpage_3">
        <h2>Page 3</h2>
        <p>Pellentesque habitant morbi tristique senectus...</p>
      </div>
    </div>
  </div>
<script src="js/puretabs.js"></script>
</body>
</html>

Open in new window

0
yando18
Asked:
yando18
1 Solution
 
dnzone88Commented:
Try changing your code
tabs[i].onclick=Site.displayPage();

Open in new window

to
tabs[i].addEventListener('click',Site.displayPage);

Open in new window


for more information on addEventListener please visit https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now