0

I have a webpage with a form that contains some input fields and one of these is a listbox. Inside this listbox I have to add (in crescent order) numbers that go from 40000 to 99999 and they increase by 1000 every time.

Example: 40000 - 41000 - 42000 - 43000 ... 97000 - 98000 - 99000 - 99999

I wrote a Javascript function but it's not working. Here you can see the HTML code:

<fieldset style="width:500px;">
<legend><font color="#D8D8D8"><b>Required Fields</b></font></legend>
<font color="#FFFFFF"><b>Player's Name</b>:</font> <input type="text" name="nome" />
<font color="#FFFFFF"><b>VRs</b>:</font> <select name="cognome">
</select> <br />
</fieldset>

Here there's the javascript function

<script>
var i=40000

for(i;i<42000;i=i+1000)
{var select = document.getElementById("cognome");
select.options[select.options.length] = new Option(i, i)}
}
</script>

My problem is that any data appear on the listbox. Do you have any suggestions?

6
  • What exactly does not working? Have you already taken a view into the error console of your Chrome, Safari or Firefox browser? Commented Apr 25, 2013 at 9:39
  • 1
    What exactly is the problem? Commented Apr 25, 2013 at 9:39
  • I put that script in <body onload="myFunction()"> but when I open the page, I don't see any new option added. I use Firefox 20.0.1 Commented Apr 25, 2013 at 9:41
  • The } on the end of new option is mismatched. Commented Apr 25, 2013 at 9:42
  • What happends if you replace the closing curly braces by a normal closing braces at 'new Option(i, i}'? ' Commented Apr 25, 2013 at 9:42

3 Answers 3

3

You're using getElementById so you need an id:

<select id="cognome" name="cognome">

Also the syntax error where the parentheses need to match :)

Sign up to request clarification or add additional context in comments.

1 Comment

well spotted - this fixes it
0

Look at your javascript error console, you misplaced a } instead of a ) , see:

select.options[select.options.length] = new Option(i, i}
                              THIS SHOULD BE A ')'-----^

Also add the id as louisbros comments in other answer..

See working demo

Comments

0

Using the comments above:

  • Adding an id="cognome"

  • Correcting the parens.

There's a JFiddle with it working here:

http://jsfiddle.net/WEd84/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.