Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. adding a item to listbox by javascript [modified]

adding a item to listbox by javascript [modified]

Scheduled Pinned Locked Moved Web Development
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    N Q H
    wrote on last edited by
    #1

    Hi, I have a form, and in this form, there's a button and a listbox. The question is how to add a item to the listbox using javascript when the user clicks on the button. This is my thinking but it doesn't work.

    var lb = document.getElementById('mylist');
    lb.options.....(what will be here?) :doh:

    Thanks in advance

    Nguyen Quang Huy

    modified on Wednesday, April 1, 2009 4:16 AM

    M 1 Reply Last reply
    0
    • N N Q H

      Hi, I have a form, and in this form, there's a button and a listbox. The question is how to add a item to the listbox using javascript when the user clicks on the button. This is my thinking but it doesn't work.

      var lb = document.getElementById('mylist');
      lb.options.....(what will be here?) :doh:

      Thanks in advance

      Nguyen Quang Huy

      modified on Wednesday, April 1, 2009 4:16 AM

      M Offline
      M Offline
      mrMercury
      wrote on last edited by
      #2

      Try the following:

      <script language="javascript">
      var lb = document.getElementById('mylist');
      var option = document.createElement("option");
      lb.options.add(option);
      option.text = "This is option one";
      option.value = "1";
      </script>

      N 1 Reply Last reply
      0
      • M mrMercury

        Try the following:

        <script language="javascript">
        var lb = document.getElementById('mylist');
        var option = document.createElement("option");
        lb.options.add(option);
        option.text = "This is option one";
        option.value = "1";
        </script>

        N Offline
        N Offline
        N Q H
        wrote on last edited by
        #3

        thank you very much, it works properly.

        Nguyen Quang Huy

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups