SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onchange event for select not firing

    This is a bit of a strange one ...

    I have two selection lists. The first one has an onchange event which causes the second selection list's contents to dynamically change. All working fine on all browsers.

    When the user has not selected the right options (left the default option) validation takes place server side and then reloads the page. The problem is that because the page is reloaded, the selection lists are back to default.

    What I *thought* was that I could add in a line onload to force the first selection list to be whatever they chose last time, e.g.

    Code:
    var s = document.getElementById('sel');
        s.onchange = function() {
            //refresh second list ...
        }
        //force the selection to change onload
        s.selectedIndex=3;
    This *does* cause the first selection list to be changed visually, but it does not trigger the onchange event - only a click from the mouse or keyboard, in other words an action by the user, seems to do that.

    Am I simply asking for something that is not possible?

    Assuming that on page reload I can identify the previously selected option's index, what technique could I use to cause that second list to be generated?

    Open to any ideas.
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    You can activate the event by calling s.onchange()
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You can activate the event by calling s.onchange()
    Ah, OK, that was simple - literally telling it after changing the index to run the onchange event? Wasn't aware that you'd need to do that, but it works a treat.

    Thanks for that - glad there was a simple answer!
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback


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
  •