SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help to trigger onchange function

    Hello everyone. I am having a bit of an issue. I am using a script I found from w3schools, but I need to add something I cant figure out how to do.

    The script is here http://www.w3schools.com/Ajax/tryit....yajax_database

    It works well, the problem is on my app, the selection is saved via database. But when the page is refreshed, the database data does not load. It only loads when the user makes a selection change.

    On the link above, try adding "SELECTED" on of the options, then hit the "edit and click me" button, you will find that no data is loaded for that selection until you make a change.

    Can this be fixed somehow? Thanks for any help!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The select box only triggers when its changed.
    Code:
    <select name="customers" onchange="showCustomer(this.value)">
    So, you need an extra piece of scripting that can check to see if the select box is on an index value that is greater than 0, and if it is it can then trigger the same onchange event.

    Give the form a unique identifier, so that the script can easily interact with it:
    Code html4strict:
    <form id="customerinfo" action="">

    Place this script at the end of the body, just before the </body> tag.
    Code javascript:
    var form = document.getElementById('customerinfo'),
        select = form.elements.customers;
    if (select.selectedIndex > 0) {
        select.onchange();
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The select box only triggers when its changed.
    Code:
    <select name="customers" onchange="showCustomer(this.value)">
    So, you need an extra piece of scripting that can check to see if the select box is on an index value that is greater than 0, and if it is it can then trigger the same onchange event.

    Give the form a unique identifier, so that the script can easily interact with it:
    Code html4strict:
    <form id="customerinfo" action="">

    Place this script at the end of the body, just before the </body> tag.
    Code javascript:
    var form = document.getElementById('customerinfo'),
        select = form.elements.customers;
    if (select.selectedIndex > 0) {
        select.onchange();
    }
    That seems to work great on the test page, thanks! I had found another solution last night but I think this is far cleaner.

    Thanks again.


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
  •