Skip to content

Two ways to add Option Tag using Javascript (part 2)

September 1, 2013

CLICK HERE FOR PART 1

CLICK HERE FOR PART 3

optiontag

There are two ways: one is by using the document.nameofform.nameofSelect.options format. the other one is by using the getElementById. Below is the script

  1. <script type = “text/javascript” language = “javascript”>
  2. //ONE WAY
  3. function birthDate_Option()
  4. {
  5. var months = new Array(‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’,
  6. ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’);
  7. document.form_signup.month.options.length = months.length;
  8. for (i = 0; i < months.length; i++)
  9. {
  10. document.form_signup.month.options[i].text = months[i];
  11. }
  12. }
  13. //ANOTHER WAY
  14. function displayResult()
  15. {
  16. var months = new Array(‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’,
  17. ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’);
  18. var x = document.getElementById(“month”);
  19. var option = new Array();
  20. for (var v = 0; v < months.length; v++) {
  21. option[v] = document.createElement(“option”);
  22. option[v].text = “options here”;
  23. x.add( option[v], null);
  24. }
  25. }
  26. </script>

Just insert the script inside the head or the body tag.

  1. <html>
  2. <head>
  3. <title>2 ways</title>
  4. </head>
  5. <!– change the function in the onload to birthDate_Option() to test the other one–>
  6. <body onload = “displayResult();”>
  7. <pre>BirthDate</pre>
  8. <select name=”month” id =’month’></select>
  9. </body>
  10. </html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

TurtleAndRobot.com

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.

paritoshmishra

Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.

ProgramThat;

A simple guide to programming...

ITechonology

Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans

Gigaom

Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media

The Neighborhood

society online's social conscious

HarsH ReaLiTy

A Good Blog is Hard to Find

VentureBeat

News About Tech, Money and Innovation

retireediary

The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

%d bloggers like this: