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.asp?filename=tryajax_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? :confused: Thanks for any help!

The select box only triggers when its changed.

<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:

<form id="customerinfo" action="">

Place this script at the end of the body, just before the </body> tag.


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. :slight_smile: