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. ASP.NET
  4. Insert values into Listbox with Javascript

Insert values into Listbox with Javascript

Scheduled Pinned Locked Moved ASP.NET
javascripttutorialquestion
5 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.
  • S Offline
    S Offline
    sbao004
    wrote on last edited by
    #1

    Hi, I want to split a sentence and put each word into a row of ListBox by using Javascript. Does anyone know how to do that? Thanks,

    A 1 Reply Last reply
    0
    • S sbao004

      Hi, I want to split a sentence and put each word into a row of ListBox by using Javascript. Does anyone know how to do that? Thanks,

      A Offline
      A Offline
      Aashutoshkumar
      wrote on last edited by
      #2

      try this for (var i = 0; i < states.Tables[0].Rows.length; ++i) { document.getElementById("ddlState").options[document.getElementById("ddlState").options.length] = new Option(states.Tables[0].Rows[i].StateName, states.Tables[0].Rows[i].StateId); } or document.getElementById("ddlState").options[0] =value; Ashutosh kumar

      S 2 Replies Last reply
      0
      • A Aashutoshkumar

        try this for (var i = 0; i < states.Tables[0].Rows.length; ++i) { document.getElementById("ddlState").options[document.getElementById("ddlState").options.length] = new Option(states.Tables[0].Rows[i].StateName, states.Tables[0].Rows[i].StateId); } or document.getElementById("ddlState").options[0] =value; Ashutosh kumar

        S Offline
        S Offline
        sbao004
        wrote on last edited by
        #3

        Hi Ashutosh, Thank you for your reply. Can I please know what "ddlState" is? Is it the name of the Listbox I want to display the words, or the control that has the sentance? If it is the Listbox, where can I pass the sentance? And should I use something to split the sentance first? Thanks,

        A 1 Reply Last reply
        0
        • S sbao004

          Hi Ashutosh, Thank you for your reply. Can I please know what "ddlState" is? Is it the name of the Listbox I want to display the words, or the control that has the sentance? If it is the Listbox, where can I pass the sentance? And should I use something to split the sentance first? Thanks,

          A Offline
          A Offline
          Aashutoshkumar
          wrote on last edited by
          #4

          yes ddlState is a id of dropdownlist. value is a variable through which we can insert data into list. Aashutosh Kumar

          1 Reply Last reply
          0
          • A Aashutoshkumar

            try this for (var i = 0; i < states.Tables[0].Rows.length; ++i) { document.getElementById("ddlState").options[document.getElementById("ddlState").options.length] = new Option(states.Tables[0].Rows[i].StateName, states.Tables[0].Rows[i].StateId); } or document.getElementById("ddlState").options[0] =value; Ashutosh kumar

            S Offline
            S Offline
            sbao004
            wrote on last edited by
            #5

            Hi Ashutosh, Sorry, I'm still not very clear about these codes Does it work the same for ListBox as well? So what is "states"? Is it a name of a talbe or a DataSet or something?

            Aashutoshkumar wrote:

            for (var i = 0; i < states.Tables[0].Rows.length; ++i)

            What about "StateName" and "StateId"? Are these some kind of a property or it will be the Name and ID of the DataSet?

            Aashutoshkumar wrote:

            document.getElementById("ddlState").options[document.getElementById("ddlState").options.length] = new Option(states.Tables[0].Rows[i].StateName, states.Tables[0].Rows[i].StateId);

            How about the "or" is it a part of the code or it's the next line of the code will do the same thing as the codes above, so it is another option for me?

            Aashutoshkumar wrote:

            or document.getElementById("ddlState").options[0] =value;

            Thank you for answering me. And sorry about the trouble, without understanding them I can not modify them and customise into my codes. Thanks again,

            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