On Load and On Change
On Load and On Change
It's not something that's ever really come up before, but I'm re-creating a desktop application as a web-app and I'm coming across a number of places where:
1. A select box may be pre-filled with data from a database, and
2. Changing that select box affects the rest of the page.
I'm looking for a neat way to perform an action that could happen when the page loads, and/or when the select changes. Knowing my luck it's something stupidly easy.
For example, some code that is currently in progress:
- switch($('option:selected',$transaction).text()){ // On Load
- case 'Comparable': case 'For Sale / To Let':
- $.each($priceFields, function(){
- $(this).addClass('short');
- });
- $.each($priceFieldsTwo, function(){
- $(this).show();
- });
- break;
- case 'For Sale':
- console.log('2');
- break;
- case 'To Let':
- console.log('4');
- break;
- };
I'm looking for a way to perform this code on initial load, and then on change of the same select, without having to write it out twice.
I suppose I could write it as a seperate function each calls but that's not quite as neat as I'd like either as I'd still need three of them for this switch.
Any ideas?