Forum Moderators: open

Message Too Old, No Replies

question on looping

         

noreaga

6:50 pm on Nov 5, 2004 (gmt 0)



Hi everyone,

Im trying to learn HTML and javascript here so please bear with me if some of my question seem rediculus.

Right then im trying to loop a listbox alert counting how many books have been selectedm for the most part this is going well and i thought i had it all just great. When i tested it i get a javascript error (on firefox) saying that "Error: document.myform.book has no properties" im confused because thsi has worked before here is what my code looks like

<html>
<head>
<title>loop try1</title>
<script language="JavaScript">
<!--
function selecttitles () {
var count = 0;
for (var i = 0; i < 7; i++)
{
if (document.myform.book[i].selected)
count +=1;
}
if (count == 1)
alert("You have selected 1 book")
else
alert("You have selected "+count+" books");
}
//-->
</script>
</head>

<body>
Choose from the following list:<p>
<form name="myform">
<select size="7" multiple>
<option value="book" selected>Programming and problem solving with Java
<option value="book">Developing Web Applications with ASP
<option value="book">Programming and Problem Solving with Visual Basic NET
<option value="book">Active Server Pages 3.0 from Scrath
<option value="book">Starting out wiht c++
<option value="book">Action Script for FlashMX
<option value="book">C++ How to Program
</SELECT>
<input name="" type="button" value="select titles" onClick="selecttitles()">
</form>

</body>
</html>

im so sorry to bother you with this but i do hope one day i can repay someone else. Anyway im sure ill be browsing the forums often now tha i know this is here.

Thanks
noreaga

kaled

7:09 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tend to refer to books and online examples, etc. when I'm writing javascript because I can never remember the DOM (document object model).

However, from memory (which may be wrong) you need to ....

name the <SELECT> object eg <SELECT name="mybooks">
refer to the mybooks.options array. The elements of this array are option objects and have "text" and "value" fields.

mybooks.length will yield the number of items.

Kaled.