[web] How do I create a Select Object with javascript?

Started by
6 comments, last by benryves 14 years, 7 months ago
I've just started learning java script and I am trying to instantiate a Select Object. The following code: SelObj=new Select(); doesn't work. How do I create a Select object?
-----------------------------Download my real time 3D RPG.
Advertisement
You have to use the createElement function.

/* create a select list element */var mySelect = document.createElement("select");  /* create some select items */var myOption1 = document.createElement("option");myOption1.text = "first item";myOption1.value = "first"  var myOption2 = document.createElement("option");myOption2.text = "second item";myOption2.value = "second"/* add the items to the list */mySelect.appendChild(myOption1);mySelect.appendChild(myOption2);/* insert the list element (let's assume the page contains a form with id "myForm" */var myForm = document.getElementById("myForm");myForm.appendChild(mySelect);
Thanks.
-----------------------------Download my real time 3D RPG.
Btw, I couldn't find that method on any of the js reference sites I'm visiting. Do you know a website that lists all the functions of specific &#106avascript objects?
-----------------------------Download my real time 3D RPG.
Quote:Original post by ManaStone
Btw, I couldn't find that method on any of the js reference sites I'm visiting. Do you know a website that lists all the functions of specific &#106avascript objects?<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>I usually use w3 schools. (http://www.w3schools.com/)
Check out the first gameplay video from my javascript/PHP RTS game
Quote:Original post by MortusMaximus
Quote:Original post by ManaStone
Btw, I couldn't find that method on any of the js reference sites I'm visiting. Do you know a website that lists all the functions of specific &#106avascript objects?


I usually use w3 schools. (http://www.w3schools.com/)


That is what I'm primarily using as well, but it doesn't list the createElement method on its document object page.
-----------------------------Download my real time 3D RPG.
Quote:Original post by ManaStone
Quote:Original post by MortusMaximus
Quote:Original post by ManaStone
Btw, I couldn't find that method on any of the js reference sites I'm visiting. Do you know a website that lists all the functions of specific &#106avascript objects?


I usually use w3 schools. (http://www.w3schools.com/)


That is what I'm primarily using as well, but it doesn't list the createElement method on its document object page.


The reference you need is here.
Check out the first gameplay video from my javascript/PHP RTS game
A nice &#106avascript toolkit (like MooTools) makes life a bit easier (and has easier to follow documentation):

<form id="myForm"></form><script type="text/javascript">window.addEvent('domready', function() {    $('myForm').grab(        new Element('select', {            events : {                change : function() {                    alert('You have selected "' + this.value + '".');                }            }        }).adopt(            new Element('option', { text : 'first item', value : 'first' }),            new Element('option', { text : 'second item', value : 'second' })        )    );});</script>

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement