Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19065

Re: How to add SELECT element and its Options dynamically?

Newsgroups comp.lang.javascript
Date 2013-03-30 18:03 -0700
References <ea514416-dcd4-4b33-a100-37ed78faa274@googlegroups.com>
Message-ID <b5fb3c2b-8bb8-467d-b718-b83da1f88b4e@googlegroups.com> (permalink)
Subject Re: How to add SELECT element and its Options dynamically?
From justaguy <lichunshen84@gmail.com>

Show all headers | View raw


Hi,

I've found the source of the issue, please see below.

>On Friday, March 29, 2013 4:40:36 PM UTC-4, justaguy wrote:
> Hi,
> 
> 
> 
> The following code generates err msg: "TypeError: x is null".
> 
> I wonder what's the correct syntax to do it.
> 
> 
> 
> Thanks as always.
> 
> 
> 
> --------------
> 
> // start
> 
> var cnt =1;
> 
> 
> 
> var cell = document.createElement("td");				

The following block of code is to create a dynamic SELECT element.
 
> var cellText = document.createElement('input');

The above code is incorrect because Select element is different from Text element, hence, using 'input' for 'select' it won't work.

So, I attempted to replace 'input' with 'select' in the above line, and yet still to no avail.

So, question, how do we create a dynamic Select element?
Thanks.

> cellText.type = 'select';
Since select has been specified I commented out the above line, and yet still to no avail.

> 
> cellText.id = 'pickUpshipTime'+cnt;
> 
> cellText.name = 'pickUpshipTime'+cnt;
> 
> cellText.size = "3";					
The above 3 lines of code for a SELECT element should be ok.
 
The following lines of code is to add options for the above newly created Select element, the code has been tested to work.


> 
> x=document.getElementById('pickUpshipTime'+cnt);
> 
> var option=document.createElement("option");
> 
> option.text="EARLY AM";
> 
> x.add(option,null);
> 
> option.text="9 am - 1 pm";
> 
> x.add(option,null);
> 
> 
> 
> // end
> 
> -----------------

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to add SELECT element and its Options dynamically? justaguy <lichunshen84@gmail.com> - 2013-03-29 13:40 -0700
  Re: How to add SELECT element and its Options dynamically? Danny <dann90038@gmail.com> - 2013-03-29 14:20 -0700
    Re: How to add SELECT element and its Options dynamically? justaguy <lichunshen84@gmail.com> - 2013-03-30 18:04 -0700
  Re: How to add SELECT element and its Options dynamically? justaguy <lichunshen84@gmail.com> - 2013-03-30 18:03 -0700
    Re: How to add SELECT element and its Options dynamically? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-03-31 18:15 +0200
  Re: How to add SELECT element and its Options dynamically? justaguy <lichunshen84@gmail.com> - 2013-03-30 18:28 -0700
    Re: How to add SELECT element and its Options dynamically? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-31 11:17 +0200

csiph-web