HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
innerHTML problem - Span not updating
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML
View previous topic :: View next topic  
Author Message
SSwan



Joined: 28 Sep 2006
Posts: 2

PostPosted: Thu Sep 28, 2006 9:44 am     innerHTML problem - Span not updating Reply with quote

I admin a site for my kids' soccer team. The home page of the site has a "Message of the day" feature which displays a random message when the page loads.

I've been trying to add a "Next Message" button to the form to allow the user to read additional messages without having to press the Reload button. When I debug the page, everything appears to be working fine, yet the text never changes on the screen despite the fact that the innerHTML value is different in the debugger.

You can view the page at: www.gscinferno.org/test.htm The relevant code follows:

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<style type="text/css" media="all">@import url(layout.css);</style>

<script type='text/javascript' src="scripts/motd.js"></script>
<script type='text/javascript'>
  var MOTDHead = "<b>Message of the Day:</b><br/>"
  var cnt=0

function rndm(a,b){
    return (Math.random()-Math.random())
}

function dispmotd(){
      msgotd.sort(rndm)
      motd.innerHTML= MOTDHead + msgotd[0]
}

function nextmsg(){
cnt=counter.value
if (cnt<msgotd.length-1)
cnt++;
else
cnt=0;
motd.innerHTML = (MOTDHead + msgotd[cnt]);
counter.value=cnt;
}

</script>

<body onLoad="dispmotd()">
<input type="hidden" name="counter" value=0>

<div id="Contents">
   <input type="button" value="Next Msg"' style="width:70;position:relative;left:250;top:25;background-color:#BFBFBF;color:#000000;font-family:Arial;font-size:9pt;border:2px outset #FFFFFF" onclick="nextmsg()">
   <span id="motd"></span>
</div>

</body>
</html>


The "motd.js" file initializes the msgotd[] array. I'm not sure whether the hidden input "Counter" object is necessary to maintain state...I've tried the code without it, but it seems that the cnt variable resets to 0 if I don't use it.

I would appreciate any insight you might have on this. It's had me scratching my head for a couple of days now.

Thanks -
Steve
skorupa



Joined: 26 Oct 2006
Posts: 1

PostPosted: Thu Oct 26, 2006 11:51 am     Reply with quote

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<style type="text/css" media="all">@import url(layout.css);</style>

<script type='text/javascript' src="scripts/motd.js"></script>
<script type='text/javascript'>
  var MOTDHead = "<b>Message of the Day:</b><br/>"
  var cnt=0

function rndm(a,b){
    return (Math.random()-Math.random())
}

function dispmotd(){
      msgotd.sort(rndm)
      getElementById('motd').innerHTML= MOTDHead + msgotd[0]
}

function nextmsg(){
cnt=counter.value
if (cnt<msgotd.length-1)
cnt++;
else
cnt=0;
getElementById('motd').innerHTML = (MOTDHead + msgotd[cnt]);
counter.value=cnt;
}

</script>

<body onLoad="dispmotd()">
<input type="hidden" name="counter" value=0>

<div id="Contents">
   <input type="button" value="Next Msg"' style="width:70;position:relative;left:250;top:25;background-color:#BFBFBF;color:#000000;font-family:Arial;font-size:9pt;border:2px outset #FFFFFF" onclick="nextmsg()">
   <span id="motd"></span>
</div>

</body>
</html>


Hi I hope this will work for You, if any questions send me a mail.
SSwan



Joined: 28 Sep 2006
Posts: 2

PostPosted: Wed Nov 08, 2006 7:53 am     Reply with quote

I had attempted to use getElementByID() prior to my original post without success & tried it again after you posted your solution just in case I missed something the first time around. No joy, unfortunately. Sad Not sure why, but the function throws an error whenever I try to use it & the whole script comes to a screeching halt. Question

I did manage to find the problem though. There was an anchor tag which was a remnant of a previous incarnation of the page whose name was conflicting with the span ID. Embarassed Once I cleaned the anchor out of there, it was off to the races.

I do appreciate the help, though. Thanks for the effort & for taking the time to work on the problem.

Steve
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Webmaster Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial