Jump to content
  • Advertisement
Sign in to follow this  
ManaStone

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

This topic is 3380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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 javascript objects?

Share this post


Link to post
Share on other sites
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 javascript objects?


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

Share this post


Link to post
Share on other sites
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 javascript 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.

Share this post


Link to post
Share on other sites
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 javascript 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.

Share this post


Link to post
Share on other sites
A nice javascript 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>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!