www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Function to dynamically create select list not working

Hybrid View

  1. #1
    Join Date
    Sep 2012
    Posts
    5

    resolved [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!

    HTML Code:
    <?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>

    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);
    }

  2. #2
    Join Date
    Nov 2012
    Location
    Jakarta
    Posts
    42
    Move

    Code:
    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.

  3. #3
    Join Date
    Sep 2012
    Posts
    5
    Thanks! This completely fixed the issue.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,500
    Please mark your post as [Resolved] then
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles