/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Function to dynamically create select list not working

I need to create functions that pull headings from an html file and create a select list out of them.

This was actually working yesterday, I have no idea what I’ve changed to cause it to stop. As of right now, the select list is created, but only contains the “Show All Character Lines” option. Nothing comes up when I debug the script.

If anyone could help me figure out what’s going wrong, I’d really appreciate it. Thanks!

[code=html]<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>

<title>The Tempest, Act V, Scene 1</title>
<link href=”plays.css” rel=”stylesheet” type=”text/css” />
<script src=”scene.js” type=”text/javascript”></script>
</head>

<body>
<div id=”linklist”>
<img src=”plays_out.jpg” alt=”The Plays” />
<img src=”son_out.jpg” alt=”The Sonnets” />
<img src=”bio_out.jpg” alt=”Biography” />
<img src=”globe_out.jpg” alt=”The Globe” />
<img src=”strat_out.jpg” alt=”Stratford” />
</div>
<div id=”title”><img src=”tempest.jpg” alt=”The Tempest” /></div>
<div id=”actList”><table><tr>
<td>ACT I</td><td>ACT II</td><td>ACT III</td>
<td>ACT IV</td><td>ACT V</td>
</tr></table></div>

<div id=”characterList”></div>

<div id=”sceneIntro”>
<h2>Lines from Act V, Scene 1</h2>
</div>

<div id=”scene”>
<h3>PROSPERO</h3>
<blockquote><i>Enter PROSPERO in his magic robes, and ARIEL</i></blockquote>
<blockquote>Now does my project gather to a head:<br />
My charms crack not; my spirits obey; and time<br />
Goes upright with his carriage. How’s the day?
</blockquote>

<h3>ARIEL</h3>
<blockquote>On the sixth hour; at which time, my lord,<br />
You said our work should cease.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>I did say so,<br />
When first I raised the tempest. Say, my spirit,<br/>
How fares the king and’s followers?
</blockquote>

<h3>ARIEL</h3>
<blockquote>Confined together<br />
In the same fashion as you gave in charge,<br />
Just as you left them; all prisoners, sir,<br />
In the line-grove which weather-fends your cell;<br />
They cannot budge till your release. The king,<br />
His brother and yours, abide all three distracted<br />
And the remainder mourning over them,<br />
Brimful of sorrow and dismay; but chiefly<br />
Him that you term’d, sir, ‘The good old lord Gonzalo;<br />
His tears run down his beard, like winter’s drops<br />
From eaves of reeds. Your charm so strongly works ’em<br />
That if you now beheld them, your affections<br />
Would become tender.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Dost thou think so, spirit?
</blockquote>

<h3>ARIEL</h3>
<blockquote>Mine would, sir, were I human.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>And mine shall.<br />
Hast thou, which art but air, a touch, a feeling<br />
Of their afflictions, and shall not myself,<br />
One of their kind, that relish all as sharply,<br />
Passion as they, be kindlier moved than thou art?<br />
Though with their high wrongs I am struck to the quick,<br />
Yet with my nobler reason ‘gaitist my fury<br />
Do I take part: the rarer action is<br />
In virtue than in vengeance: they being penitent,<br />
The sole drift of my purpose doth extend<br />
Not a frown further. Go release them, Ariel:<br />
My charms I’ll break, their senses I’ll restore,<br />
And they shall be themselves.
</blockquote>

<h3>ARIEL</h3>
<blockquote>I’ll fetch them, sir.</blockquote>

<blockquote><i>Exit</i></blockquote>

</body>
</html>[/code]

JS:

[CODE]/*
Functions:
uniqueElemText(elemName)
Returns the unique content from HTML tags with the
tag name elemName. The list is sorted in alphabetical
ordered and returned as an array.

*/

function addEvent(object, evName, fnName, cap) {
if (object.attachEvent)
object.attachEvent(“on” + evName, fnName);
else if (object.addEventListener)
object.addEventListener(evName, fnName, cap);
}

addEvent(window, “load”, makeList, false);
var sourceDoc = document.getElementById(“scene”);

function uniqueElemText(elemName) {
elems = document.getElementsByTagName(elemName);
elemsArray = new Array();

for (var i=0; i<elems.length; i++) elemsArray[i]=elems[i].innerHTML;
elemsArray.sort();
for (i=0; i<elemsArray.length-1; i++) {
if (elemsArray[i]==elemsArray[i+1]) {
elemsArray.splice(i+1,1);
i–;
}
}
return elemsArray;
}

characterNames = uniqueElemText(“h3”);

function makeList () {
var listBox = document.getElementById(“characterList”);
listBox.innerHTML = “<p>Show Only Lines By:</p>”;
var listBoxChars = document.createElement(“select”);
listBoxChars.id = “cList”;
listBox.appendChild(listBoxChars);

var opt0 = document.createElement(‘option’);
listBoxChars.appendChild(opt0);
opt0.innerHTML = opt0.innerHTML + “Show All Character Lines”;
opt0.id = “opt0”;

for (var i=0; i<characterNames.length; i++){
var opt = document.createElement(‘option’);
listBoxChars.appendChild(opt);
opt.innerHTML = opt.innerHTML + characterNames[i];
opt.id = “opt” + (i+1);
}

//addEvent(listBox, “focus”, testest, false);
}
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@firesnakerDec 11.2013 — Move

characterNames = uniqueElemText("h3");

to inside the function makeList()

Variables inside function are local.

So, if you define a variable outside a function and use it inside a function, it is not going to be recognized.
Copy linkTweet thisAlerts:
@taymaxiauthorDec 12.2013 — Thanks! This completely fixed the issue. ?
Copy linkTweet thisAlerts:
@rootDec 13.2013 — Please mark your post as [Resolved] then ?
×

Success!

Help @taymaxi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...