SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot bensheard's Avatar
    Join Date
    Jun 2006
    Location
    Cape Town | South Africa
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Execute onchange event through javascript

    Hi
    I have a page where you can edit information that you have already submitted into a database. The form has two drop down menu, and the first one populates the second when the onchange event is excecuted. But on the edit page, I use javascript to select the value from the first drop down menu, but it doesnt execute the onchange event so the second menu stays blank so I cant select the existing value from it. Is there any way I can do this, someting like:
    Code:
    document.f.menu1.change();
    document.f.menu2.value = existing_value;
    thanks
    Ben Sheard
    -------------
    Log all your dives easily and quickly from anywhere in the world
    www.divinglogs.cafe150.com

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use selectedIndex and give it a int value. I'm sure this fires the event.


  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use javascript to select the value on your edit page, can't you use the same event to fire off whatever function your onChange event is supposed to call?

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    although alot of people use the selectedIndex and SelectedValue values to determin what to do in the function, so simply calling this function would only break or use the value that is currently selected.

    Where as, if you programatically set the selectedIndex it changes the value and fires the event.


  5. #5
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts
    . . . and fires the event.
    While selectedIndex is read/write and can be modified, I don't believe it will fire the event. It would be highly unusual for a function to fire events that are supposed to result from user action, even if the result of the script is the same as the result of the action. Even the submit() method doesn't fire the onsubmit event.

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I stand corrected, I tested my theory and nothing. It must be my exposure to C# as if I set the selectedIndex of an control it fires the selectedIndexChanged event.

    Pah, either way you can set the selectedIndex and then call the function that deals with the value. Resulting in rinpoche's answer being valid as well.



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
  •