Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Your combobox's name is meals and not Cookie.

    textarea has no value attribute. So, this part is erroneous:
    Code:
    document.text1.cookies.value = Cookie[document.text1.text2.value];
    You might want to choose input tag for this instead.

    Also, center is a deprecated tag.

    Hope it keeps you going.
    Edit:
    My bad!
    Code:
    document.text1.cookies.value
    This should have been corrrect. It's just it is outside the form.
    Last edited by rangana; 06-07-2008 at 09:21 AM.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    selectedIndex internal variable

    Thanks Rangana,

    What do you mean by your "combobox's name"

    When you say the text area has no attributes, do you refer to the text box, if so, it has, I put it in red color.

    When you say "this part is erroneous" could you clarify.... still new in this....


    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>

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    1. Combobox is closely related to drop-down-lists sorry to confused you.

    2. Sorry. My bad. See edition on post#2.

    Here's a working code:
    Code:
    <HTML>
    <HEAD><title> Assign 2</title>
    
    <script type="text/javascript">
    <!-- Beginning of JavaScript -->
    
    function MsgBox (textstring) {
    alert (textstring) }
    
    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() {
    document.text1.cookies.value=document.text1.text2.value+' '+document.text1.meals.options[document.text1.meals.selectedIndex].value;
    }
    
    </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>
    <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>
    </FORM>
    </SCRIPT>
    </BODY>
    </HTML>
    PS, see link on deprecated attributes.

    Hope it helps.

    Edit:
    My bad, the real reason is that the textarea is outside the form, the reason behind why it throws an error.

    Thanks coothead.
    Last edited by rangana; 06-07-2008 at 09:24 AM. Reason: coothead is just a good guy.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    selectedIndex internal variable

    Thanks again rangana.

    I think that I understand a little bit more.

    One thing. In the text area when you click the "Click here for Fortune" button you should see the user name and one of the cookies from the array randomly pick by the computer, but now is showing me the food that I selected. How can I change that. I tried using the name of the array or the variable but nothing so far.

    Thanks for the link. I changed the deprecated coding.

  • #6
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    selectedIndex internal variable

    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>
    Last edited by nogal; 06-08-2008 at 07:19 AM. Reason: missing /code close tag

  • #7
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    I don't know what your "plan" is, but you have the script to validate before submit, but you're not calling it.

    You also don't have a "submit" button, but an "ordinary" button having a value of "Submit Order".

    Highlighted is erroneous:
    Code:
    function showcookies() {
    document.text1.cookies.value=document.text1.text2.value+' '+document.text1.indexCookie.options[document.text1.indexCookie.selectedIndex].value;
    }
    Your drop-down-list's name, as I say is meals and not indexCookie.

    This new edition, including the Cookie flaw "might" help you:
    Code:
    <HTML>
    <HEAD><title> Assign 2</title>
    
    <script type="text/javascript">
    <!-- Beginning of JavaScript -->
    
    function MsgBox (textstring) {
    alert (textstring) }
    
    var indexCookie=null;
    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.meals.options[document.text1.meals.selectedIndex].value;
    }
    function checkFormResp(){
    	if (document.text1.text2.value== ""){
    	alert("Please put your name, otherwise the form will NOT be sent");
    	return false;
    	}
    
    	}
    
    </script>
    
    </script>	
    </HEAD>
    <BODY>
    
    <h1><div style="text-align:center">Chinese Food</h1>
    <h2>Fortune Restaurant</h2>
    <p><FORM name ="text1" onsubmit="return checkFormResp()">
    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>
    
    
    <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="submit" value="Submit Order"> </p>
    <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>");
    var x=0;
    while(x<5)
    {
    document.write("The Fortune Cookie is: " + Cookie[x] + "<br><br>");
    x++;
    }
    
    </SCRIPT>
    
    <a href="myscript">Click here to go to myscript file</a><br><br>
    
    </BODY>
    </HTML>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #8
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    selectedIndex internal variable

    Sorry if I confused you... But I am even more confused. You are probably right about where the different parts of the code should go.

    The idea is that when you click on "click here for fortune" button, in the text area will appear the user name and one of the fortune cookies from the array that the computer picks randomly and no the food selected.

    At this point please do correct me if I am wrong but do I have to put the function showcookies() and the array inside the form or it is ok to put it outside and just call them when needed.

    I have been reading about how to validate forms in this website
    http://www.webcredible.co.uk/user-fr...vascript.shtml
    but it looks like I am missing something or I don't understand it completely.

  • #9
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Then, you need to generate the "cookie" array randomly:
    Code:
    <HTML>
    <HEAD><title> Assign 2</title>
    
    <script type="text/javascript">
    <!-- Beginning of JavaScript -->
    
    function MsgBox (textstring) {
    alert (textstring) }
    
    var indexCookie=null;
    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() {
    var x=Math.floor(Math.random()*Cookie.length);
    document.text1.cookies.value=document.text1.text2.value+' '+Cookie[x];
    }
    function checkFormResp(){
    	if (document.text1.text2.value== ""){
    	alert("Please put your name, otherwise the form will NOT be sent");
    	return false;
    	}
    
    	}
    
    </script>
    
    </script>	
    </HEAD>
    <BODY>
    
    <h1><div style="text-align:center">Chinese Food</h1>
    <h2>Fortune Restaurant</h2>
    <p><FORM name ="text1" onsubmit="return checkFormResp()">
    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>
    
    
    <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="submit" value="Submit Order"> </p>
    <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>");
    var x=0;
    while(x<5)
    {
    document.write("The Fortune Cookie is: " + Cookie[x] + "<br><br>");
    x++;
    }
    
    </SCRIPT>
    
    <a href="myscript">Click here to go to myscript file</a><br><br>
    
    </BODY>
    </HTML>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    nogal (06-09-2008)

  • #10
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    selectedIndex internal variable

    Thanks rangana. I got it. Now my project is working fine.

    thanks for your support and will see you next time.


  •  

    Posting Permissions

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