SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    obtain selected text

    I have a select box
    HTML Code:
    <select name="provider_id" id="Username" class="select"> 
                        <option value=4>anyone</option> 
    <option value=2>Balena</option> 
    <option value=3>Eschimossa</option> 
    <option value=1>Sandra J.</option> 
                        </select>
    I'm trying to get the selected text in the selection.

    I gather I do something like
    ... onchange="this.options[this.selectedIndex].text">
    How do I store that value in a variable so I can display it on the next page (once the form is submitted)

    Thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    When you say next page do you mean that the page refreshes to show the next part of the form? If so you can use JavaScript to keep the value stored by itself, you would need to use something like the HTML5 localStorage API to keep the value stored away in the users browser which allows you to retrieve/update/delete it at any point in time.

    For example you could use the following to set the value, the key is based on the input field name.
    Code JavaScript:
    function onSelectChange() {
        localStorage.setItem(this.name, this.options[this.selectedIndex].value);
    }

    To retrieve the value use something like the following.
    Code JavaScript:
    function getStoredItem(key) {
        return localStorage.getItem(key);
    }
     
    alert(getStoredItem('provider_id'));

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,644
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanksss
    "Oh, and Jenkins--apparently your mother died this morning."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •