Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-23-2010, 10:29 PM   PM User | #1
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
multiple functions called same time causing issue

Hey all,

I have an application.js file. The javascript is just a typical hide/show tabs
effect, so when the user clicks on one link, the corresponding div is
revealed and the other divs are hidden. Two pages require the
javascript. However, while one page displays content of no tabs onload,
the other page displays the content of the first tab onload. So the page
that displays the content of no tab onload works fine. However, the page
that displays the content of first tab onload is not working. I get the
following error in FireBug:

document.getElementById("tabs") is NULL
var tabListItems = document.getElementById("tabs").childNodes;

I think the issue is I'm calling multiple functions on page load (and
hence since the one page doesn't have a tabs id it doesn't know what to
do), even though I only need one function for each of the pages. But I
would like to keep this tab effect in the same file. So I don't know what
to do. Thanks for anyone's response:
Code:
window.onload = init;

var tabLinks = new Array();
var contentDivs = new Array();

function init() {
  initProduct();
  initAbout();
}

function initProduct() {
  var tabListItems = document.getElementById("tabs").childNodes;
  for(var i=0; i < tabListItems.length; i++) {
    if(tabListItems[i].nodeName == "LI") {
      var tabLink = getFirstChildWithTagName(tabListItems[i], "A");
      var id = getHash(tabLink.getAttribute("href"));
      tabLinks[id] = tabLink;
      contentDivs[id] = document.getElementById(id);
      tabLinks[id].className = "";
      contentDivs[id].className = "tabContent hide";
      tabLinks[id].onclick = showTab;
      tabLinks[id].onfocus = function() { this.blur() };
    }
  }
}

function initAbout() {
  var tabListItems = document.getElementById("tabAbout").childNodes;
  for(var i=0; i < tabListItems.length; i++) {
    if(tabListItems[i].nodeName == "LI") {
      tabLink = getFirstChildWithTagName(tabListItems[i], "A")
      id = getHash(tabLink.getAttribute("href"));
      tabLinks[id] = tabLink;
      contentDivs[id] = document.getElementById(id);
    }
  }
  var i = 0;
  for(var id in tabLinks) {
    tabLinks[id].onclick = showTab;
    tabLinks[id].onfocus = function() { this.blur() }
    if(i == 0) {
      tabLinks[id].className = "selected"
    }
    i++
  }
  var i = 0;
  for(var id in contentDivs) {
    if(i != 0) {
      contentDivs[id].className = "tabContent hide";
    }
    i++
  }
}

function showTab() {
  document.getElementById("product").className = "tabContent hide"
  var selectedId = getHash(this.getAttribute("href"));

  for(id in contentDivs) {
    if(id == selectedId) {
      tabLinks[id].className = "selected";
      contentDivs[id].className = "tabContent";
    }
    else {
      tabLinks[id].className = "";
      contentDivs[id].className = "tabContent hide";
    }
  }

  return false;
}



function getFirstChildWithTagName(element, tagName) {
  for(var i=0; i < element.childNodes.length; i++) {
    if(element.childNodes[i].nodeName == tagName) {
      return element.childNodes[i];
    }
  }
}

function getHash(url) {
  var hashPos = url.lastIndexOf("#");
  return url.substring(hashPos + 1);
}
johnmerlino is offline   Reply With Quote
Old 05-23-2010, 11:34 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
you can always check, if the element exists.
PHP Code:
function initProduct() {
  var 
tabList document.getElementById("tabs");
  if (!
tabList) {
    return 
false;
  }
// further code 
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-23-2010, 11:57 PM   PM User | #3
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Should this have made the error go away and make all tabs but selected one display:

Code:
function init() {
	initProduct();
	initAbout();
}

function initProduct() {
	var tabListItems = document.getElementById("tabs").childNodes;
	if (!tabListItems) {
		return false;
	}
	for(var i=0; i < tabListItems.length; i++) {
		if(tabListItems[i].nodeName == "LI") {
			var tabLink = getFirstChildWithTagName(tabListItems[i], "A");
			var id = getHash(tabLink.getAttribute("href"));
			tabLinks[id] = tabLink;
			contentDivs[id] = document.getElementById(id);
			tabLinks[id].className = "";
			contentDivs[id].className = "tabContent hide";
			tabLinks[id].onclick = showTab;
			tabLinks[id].onfocus = function() { this.blur() };
		}
	}
}
Because the error still displays and the other tabs still do not hide. So I'm wondering if there is something else wrong?

Thanks for any response.
johnmerlino is offline   Reply With Quote
Old 05-24-2010, 12:34 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by johnmerlino View Post
Should this have made the error go away and make all tabs but selected one display:
it should make the error go away.

Quote:
Originally Posted by johnmerlino View Post
Because the error still displays and the other tabs still do not hide. So I'm wondering if there is something else wrong?
compare your code to mine and you’ll see what went wrong.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
johnmerlino (05-24-2010)
Old 05-24-2010, 01:35 AM   PM User | #5
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Thanks, I see now.
johnmerlino is offline   Reply With Quote
Old 05-24-2010, 11:16 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
another possibility would be
PHP Code:
function initProduct() {
    try {
        var 
tabListItems document.getElementById("tabs").childNodes;
    }
    catch (
e) {
        
console.log(e.message); // <-- not IE compatible
        
return false;
    }
// further code 
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
divs, javascript, multiple functions, onload, tabs

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:44 PM.


Advertisement
Log in to turn off these ads.