Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,337 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,101 people online right now. Registration is fast and FREE... Join Now!




Pressing button more than once causes errors

 
Reply to this topicStart new topic

Pressing button more than once causes errors, Need continuous results for Magic 8-ball-styled program

mikjall77
post 1 Oct, 2008 - 05:20 AM
Post #1


New D.I.C Head

*
Joined: 18 Apr, 2007
Posts: 46



Thanked 1 times
My Contributions


I am developing a Magic 8-ball-styled program and am having problems getting the button to get the answer to work more than one time. It works the first time (after the initial page load and first question typed) even though it does fire an error. After that, it doesn't do anything. I don't really want to reload the page. I still have to write the code to make sure the visitor hasn't asked the same question twice in a row (wanted to get over this hurdle first). The first part of the code checks to see if the visitor included a question mark if you are wondering. Any help is greatly appreciated. If you need more info, please let me know.
CODE

<body>
<form name="frmMagicBall">
    function MagicAnswers() {    
        var txtLastCharacter = "";
        var txtCharChk = "";
        var txtUsrQuestion = document.frmMagicBall.txtQuestion.value;
        
        for (i=0;i<txtUsrQuestion.length;i++)
        {
            txtCharChk = txtUsrQuestion.charAt(txtUsrQuestion.length-1)
            if (txtCharChk != "?"){
                alert("Please add a question mark to your question.");
                
                document.frmMagicBall.txtQuestion.select();
                document.frmMagicBall.txtQuestion.focus();
                document.getElementById("spnAnswer").innerHTML = "Ask the 8 Ball a question...";
                return;
            }else{
                var newSpan = document.createElement("span");
                var newText = document.createTextNode(answers[rNum]);
                newSpan.appendChild(newText);

                var para = document.getElementById("answertxt");
                var spanElm = document.getElementById("spnAnswer");
                var replaced = para.replaceChild(newSpan,spanElm);            }
        }
        document.frmMagicBall.txtQuestion.select();
        document.frmMagicBall.txtQuestion.focus();
...
  <input type="button" name="btnAnswer" id="btnAnswer" value="Ask the 8 Ball" onclick="MagicAnswers();"/>
...
<marquee width="250px" ><p id="answertxt" style="font-size:24px"><span id="spnAnswer"><strong>
  <em></em></strong></span></p></marquee>
...
</form>
</body>
User is offlineProfile CardPM

Go to the top of the page

xerxes333
post 1 Oct, 2008 - 06:44 AM
Post #2


D.I.C Head

Group Icon
Joined: 5 Jul, 2007
Posts: 146



Thanked 7 times

Dream Kudos: 25
My Contributions


It works fine the first time because there is an element named "spnAnswer" but the second execution fails because you overwrite the "spnAnswer" span with a new span node and the new node is not named so
CODE
var spanElm = document.getElementById("spnAnswer");
gets null and
CODE
var replaced = para.replaceChild(newSpan,spanElm);
errors. You can name the new span but I would just suggest replacing the innerText of the "spnAnswer" node that way you're not creating and replacing nodes all over the place. Hope this helps.

Forgot to add:

Whats with the for loop? Why do you need to loop through the entire length of the input string? You should be OK with just checking the last character of the string to see if it is a "?" and proceeding.
User is offlineProfile CardPM

Go to the top of the page

mikjall77
post 2 Oct, 2008 - 03:04 AM
Post #3


New D.I.C Head

*
Joined: 18 Apr, 2007
Posts: 46



Thanked 1 times
My Contributions


Thanks for the Help!

I was making it much more complicated that what it should've been on it. It works great now with a lot less code. With your comment to the For loop, I don't know what I was thinking, but I did switch that also. Now all I have to do is figure out how to check if the same question has been asked twice in a row and I'm done..... tongue.gif

Thanks again
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 03:31AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month