Forum Moderators: open

Message Too Old, No Replies

Unordered List not Working in FF

Works ok in IE not in FF (null value)

         

rutabaga

8:46 pm on Sep 1, 2009 (gmt 0)

10+ Year Member



I've been working quite awhile on this problem. I recently took the original script/html from online & started to make changes one by one for my own website into the original script. (Previously, I built my website based on the script & it ran fine on IE but not on FF. I ran firebug & found the problem area, but have not discovered why so have not been able to fix it.) Now I have determined the reason for the problem of null value in my variable "currentTarget". When I made a change to an Id in the unordered list or added to the list, at the same time changing the link, is when the null value started to appear in FF—but not in IE. Does anyone have any idea why changing or adding to an unordered list would create this null value in FF?

Here is my html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>

<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="script.js"></script>
<!--<script type="text/javascript"> -->
<!--<style type="text/css"> -->
</head>

<body>

<div id="header">
<div id="scatleaImage">
<h2>Scattered Leaves</h2>
</div>

<p class="styleBookName">by J. D. Roque</p> <br />

<div id="chapterSamples">Samples of Stories
<div id="storyNames">
<ul id="chapters">
<li><a href="#The Word">The Word</a></li>
<li><a href="#lorem">lorem</a></li>
<li><a href="#ipsum">Ipsum</a></li>
<li><a href="#dolor">Dolor</a></li>
<li><a href="#sit">Sit</a></li>
<li><a href="#What Goes Around">What Goes Around</a></li>
</ul>
</div>
</div>
</div>

<div id="maincontent">
<h2 class="aboutStories">Samples</h2>

<p id="The Word" class="content"><strong><em> The Word:&nbsp; </em></strong>"At our house</p>
<p id="lorem" class="content"><strong><em> The Word: &nbsp; </em></strong>"At our house, the penalty for uttering <em>the word</em>
is to get your mouth washed out with the yellow Fels Naphtha soap Ma uses when she mops the wood floors." <br />
</p>
<p id="ipsum" class="content"><strong><em> Ipsum: &nbsp; </em></strong>In hac habitasse platea dictumst. Aenean tempor mattis arcu.</p>
<p id="dolor" class="content"><strong><em> Dolor: &nbsp; </em></strong>Maecenas sit amet turpis. Vivamus ac justo. Fusce in tortor.</p>
<p id="sit" class="content"><strong><em>Sit: &nbsp; </em></strong>Duis ornare urna et velit. Phasellus vulputate turpis vitae quam.</p>
<p id="What Goes Around" class="content"><strong><em>What Goes Around:&nbsp; </em></strong>"Although"</p>
</div>
</body>
</html>

Here is the jscript:

function addNormalClass() {
this.className = 'normal';
}

function highlightTarget() {
// Collect the relevant items:
var contentsArticles = document.getElementById('maincontent').getElementsByTagName('p');
// Loop through them:
for (var j=0;j<contentsArticles.length;j++) {
if (contentsArticles[j].className == 'highlighted') {
// If one of them has a class name of 'highlighted,' change it to 'normal':
contentsArticles[j].className = 'content';
}
}

// What is the url of the clicked link?
var url = this.href;
// Get everything after the '#' in the link--that's our id
var elementId = url.substring(url.lastIndexOf('#') + 1);
// Get the element:
var currentTarget = document.getElementById(elementId);
// Change its classname:
alert(currentTarget);
currentTarget.className = 'highlighted';
alert(currentTarget.className);
}

function buildContentsArray() {
// First collect all the links to contents and the contents they link to:
var contentsLinks =
document.getElementById('chapters').getElementsByTagName('a');
var contentsArticles =
document.getElementById('maincontent').getElementsByTagName('p');

// Loop through the links:
for (var i=0;i<contentsLinks.length;i++) {
// Call a function when the links are clicked AND when a highlighted article is clicked:
contentsLinks[i].onclick = highlightTarget;
contentsArticles[i].onclick = addNormalClass;
}
}

window.onload = buildContentsArray;

This is the css code for highlighted:

.highlighted {
background-color: #CCFFFF;
padding: 0px 20px 0px 20px;
margin: 5px 5px 10px 5px;
font-family: Arial, Helvetica, sans-serif;
color: #BCBCBC;
}

daveVk

11:25 pm on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try removing spaces for id/url

eg
<li><a href="#WhatGoesAround">What Goes Around</a></li>
...
<p id="WhatGoesAround" class="content"><strong><em>What Goes Around:&nbsp;

rutabaga

7:17 pm on Sep 2, 2009 (gmt 0)

10+ Year Member



Thanks for the reply. The spaces you're referring to I believe are "&nbsp"? They are in each of the paragraphs and the paragraphs that work are the original ones I typed in, ie, ids: lorem, ipsum, dolor & sit, which work fine. Then I added ids: The Word & What Goes Around & Mozilla finds a null value in the variable "currentTarget" (for highlighting the paragraph)just in those added ones. Very mysterious & it's driving me a bit bonkers. I can't find any rational answer. Hope you still can help out there.

whoisgregg

7:36 pm on Sep 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Spaces aren't an allowed character in the ID attribute... Nor are html entities ( like & nbps ; ).

You'll have to remove the spaces from both places that daveVk specified for those to work.

rutabaga

9:03 pm on Sep 2, 2009 (gmt 0)

10+ Year Member



Thanks...I removed all the nbps's; I don't know what other spaces are in the id attribute. The ids: lorem, ipsum, dolor & sit, still work in Mozilla; but the ids: The Word & What Goes Around still don't work. All still work in IE. The null value keeps coming up for the "currentTarget" variable in Mozilla for The Word & What Goes Around. I googled all kinds of scenarios, but have not come up with why there's a problem in FF for an unordered list using my javascript. Hope you have another suggestion.

daveVk

10:27 pm on Sep 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The Word & What Goes Around still don't work

This suggests to me there is still spaces ( TheWord NOT The Word ) or other punctuation or special characters in there. Stick to letters and numbers only. The test must be both a valid Id and valid url part.

Add alert
alert(elementId);
check for special characters and that upper/lower case is correct TheWord vs THEWORD vs theword etc

rutabaga

10:43 pm on Sep 2, 2009 (gmt 0)

10+ Year Member



daveVk--thanks so much! I did a quick change to the existing "play" website as you suggested...and voila, it worked! I have spent hours on this, cuz I didn't want to give up. Now I will go back to build my real pages, finally. Thanks so very much for your persistence & patience.

rutabaga

8:45 pm on Sep 3, 2009 (gmt 0)

10+ Year Member



I checked W3c Id attribute rules online, where it specifies what you can use, including underscores & hyphens, so that's good to know as well. My books do not mention any Id rules at all--kind of important. All the searches I did online never mentioned it either. Again, many thanks.

daveVk

2:54 am on Sep 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need also be a bit careful in this case as you are using the same text in the URL href="#...", and some special characters need encoding in the URL, underscores & hyphens should be safe.

Being conservative, I would avoid the hyphen as it to easy to confuse with the minus operator.

rutabaga

8:28 pm on Sep 4, 2009 (gmt 0)

10+ Year Member



Thanks for the good advice...