Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    resetting select/option element to default option?

    I have a select/option element in XHTML markup.
    Each option has an object method called by event handler for event 'change'
    (or 'onchange').
    Since the event being watched is 'change', if the user tries to run the
    attactched event handler again, it will not work because there is no 'change'
    event triggered.
    So... how can I reset the select/option to the default value so the user has
    to open the list and choose the methods to be called (so a change event is triggered)
    In review of the markup pattern for select/option, one option is to be
    given the attribute ... select="selected", But what would be the default
    attribute/value for the parent element (select).

    Thnx JK

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    ???? You can't "reset" the <select> without changing what is visible to the user.

    But if that is okay, then you can reset it by simply setting the selectedIndex property to zero.

    This assumes, of course, that the first <option> was the one selected by default when the page first appeared.

    If the page STARTED OUT with something like
    Code:
        <select name="frap">
            <option value="731"> zamboni </option>
            <option value="999"> pizza </option>
            <option value="0" selected> the winner </option>
       </select>
    then you'd have to use document.forms[0].frap.selectedIndex=2 to reset to the initial state.

    Have I utterly missed your point?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by anotherJEK View Post
    Each option has an object method called by event handler for event 'change'
    (or 'onchange').
    That doesn't make sense - the change event applies to the entire select so you can't test for it on individual options.

    If they don't change the option then there is no need to run the code attached to the change event again as nothing has changed.

    If you need something to run when they move to a different field regardless of whether they changed the selected option or not then you could use the blur event.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    anotherJEK (11-26-2012)

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    O.k...

    selectIndex should be the ticket.... thanx

    The select/option element is to allow the user to choose a field or fields to
    clear. So the change event calls the methods to clear the selected fields.
    As a safe guard, I want the selected item to change back to the default,
    causing the user to generate another change event to run the choice.

    I could use a click event, but I think that would require a separate
    button or other click-able element and another step.

    If the default isn't specified, the first element is the default
    Yes I want the display to revert to the default item.

    Code:
    <select id="(tag to use in document.getElementById())" name="(so I'll give it a name)">
    <option value="none">Chose a field to clear</option>
    <option value="history">clear history</option>
    <option value="memory">Clear Memory</option>
    <option value="both">Clear Both</option>
    </select>

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    Yes.

    Now explain why the <select> needs an id?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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