SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    selectedIndex internal variable

    Hi,

    this page is giving me 1 error on line 49
    " 'document.text1.Cookie.selectedIndex' is null or not an object"
    I tried many different options (that I know) but so far, I could not find why.

    the idea of this page is that you put your name in the field, then you select a food from the drop down list, after that, you click "click here for fortune" and in the text box will be display the name plus the cookie, finally you click on "submit order"

    View fortune here--> this line should go on the left bottom corner of the text box but no luck in position it there.

    I will appreciate any help.

    Thanks


    Code:
    <HTML>
    <HEAD><title> Assign 2</title>
    
    <script type="text/javascript">
    <!-- Beginning of JavaScript -
    
    function MsgBox (textstring) {
    alert (textstring) }
    
    </script>
    </HEAD>
    <BODY>
    
    <h1><center>Chinese Food</h1>
    <h2>Fortune Restaurant</h2>
    <p><FORM name ="text1">
    Please enter your name:  <input name="text2" type="text">
    <br>
    
    Pick something from the Menu: <select name="meals" size="1">
      			<option value="Wonton Soup">Wonton Soup</option>
      			<option value="Egg Rolls">Egg Rolls</option>
      			<option value="Chow Mein">Chow Mein</option>
     			<option value="Xang Su Ya">Xang Su Ya</option>
    			<option value="Dou Ban Yu">Dou Ban Yu</option>
    			</select><br><br>
    
    </center>
    </FORM>
    
    <script type="text/javascript">
    var Cookie = new Array(5);
    Cookie[0] = "An opportunity will knock on your door";
    Cookie[1] = "Friendship is more than a hand";
    Cookie[2] = "Be thankfull everyday";
    Cookie[3] = "Be impecable with your words";
    Cookie[4] = "Give always the best of you";
    
    var indexCookie=null;
    
    function showcookies() {
    
    myIndexCookie = document.text1.Cookie.selectedIndex;
    document.text1.cookies.value = cookie[document.text1.text2.value];
    }
    
    </script>
    <center>
    
    <p><input type="button" value="Click here for Fortune" onClick="showcookies();"></p>
    
    <p>View fortune here--><textarea name="cookies" rows="6" cols="40"> </textarea> </p>
    
    <p><input type="button" value="Submit Order" onClick="showcookies();"> </p>
    
    
    </center>
    </SCRIPT>
    </BODY>
    </HTML>

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The last 3 form fields are outside the form. Close the form after those tags and it should work better.

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.

    Well, now my code is a little bigger.

    Additional to what I wrote in my last posting. Now I have this:

    In red color. I am trying to validate the input from the user so always the user should put the name there otherwise the form will not be sent.... I am stuck here.

    In green color. I am trying to display the cookies that are in the array in different lines.... so far I got that but in the same line all..... please advice

    Thanks


    Code:
    <HTML>
    <HEAD><title> Assign 2</title>
    
    <script type="text/javascript">
    <!-- Beginning of JavaScript -->
    
    function MsgBox (textstring) {
    alert (textstring) }
    
    var indexCookie=null;
    
    </script>	
    </HEAD>
    <BODY>
    
    <h1><div style="text-align:center">Chinese Food</h1>
    <h2>Fortune Restaurant</h2>
    <p><FORM name ="text1">
    Please enter your name:  <input name="text2" type="text">
    <br>
    
    <script type="text/javascript">
    var Cookie = new Array(5);
    Cookie[0] = "An opportunity will knock on your door";
    Cookie[1] = "Friendship is more than a hand";
    Cookie[2] = "Be thankfull everyday";
    Cookie[3] = "Be impecable with your words";
    Cookie[4] = "Give always the best of you";
    
    function showcookies() {
    document.text1.cookies.value=document.text1.text2.value+' '+document.text1.indexCookie.options[document.text1.indexCookie.selectedIndex].value;
    }
    
    </script>
    
    Pick something from the Menu: <select name="meals" size="1">
      			<option value="Wonton Soup">Wonton Soup</option>
      			<option value="Egg Rolls">Egg Rolls</option>
      			<option value="Chow Mein">Chow Mein</option>
     			<option value="Xang Su Ya">Xang Su Ya</option>
    			<option value="Dou Ban Yu">Dou Ban Yu</option>
    			</select><br><br>
    
    
    
    <p><input type="button" value="Click here for Fortune" onClick="showcookies();"></p>
    <p>View fortune here-->
    
    <textarea name="cookies" rows="6" cols="40"> </textarea></p>
    
    <p><input type="button" value="Submit Order" onClick="showcookies();"> </p>
    
    <script type="text/javascript">
    function checkFormResp (){
    
    	if (document.text1.text2.value != " "){
    	alert("Please put your name, otherwise the form will NOT be sent");
    	return false;
    	}
    
    	}
    
    </script>
    
    <br>
               <img src="chinese food.gif" width="250" height="200"/>
               
               <br><br>
    
    </div>
    </FORM>
    
    <hr>
    
    <script type="text/javascript">
    document.write("<b>This file is being viewed using the browser: </b>" + navigator.appName +"<br><br>");
    
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write("Today is: " + currentTime.toLocaleString().bold() + "<br>");
    
    document.write("<br> this file was last modified on: " + document.lastModified + "<br><br>");
    
    for (x = 0 ; x <= 5 ; x++); {
    document.write("The Fortune Cookie is: " + Cookie + "<br><br>");}
    
    </SCRIPT>
    
    <a href="myscript">Click here to go to myscript file</a><br><br>
    
    
    </BODY>
    </HTML>

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Get rid of the onclick from the button and set an onsubmit event on the form instead.
    The for loop has a syntax error causing it to finish before the document.write statement occurs, and you're missing an array index on the Cookie variable.

    That should get your assignment working better.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks pmw57. I got it. now it is working fine.

    Thanks for your support and will see you next time.


Bookmarks

Posting Permissions

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