tags:

views:

56

answers:

4

Good Day.

I'm looking for switch replacement in jQuery. Basically, I have no idea how to replace switch.

I got following switch, for about 70 countries. Is there any way to replace it with a loop?

$("#country").change(function () {
  switch ($('#country :selected').val()) {
  case 'pl':
    $("fieldset#state").hide().load('pl.txt').fadeIn(800);
    break;
  }
});

Also, is there any possibility to implement loading specific file automatically if there's already selected item?

Edit:

My description of the problem was not the best. Sorry for that. The main problem is:

  • I have lists only for some countries, not for all of them
  • I was using switch for country to read file, if there was no, I was inserting text field by default
  • I also need to implement loading file by default. What do I mean? I query database for country and then I'm selecting it on country drop down list. How to load file automatically (if any)?

Regards, Tom

+1  A: 

How about this, it assumes you follow the convention of using the country code "pl" in the name of the text file "pl.txt" ...

$("#country").change(function () {
    var fileName = $('#country :selected').val() + '.txt';
    $("fieldset#state").hide().load(fileName).fadeIn(800);
});
Sohnee
A: 

Can you make an associative array (ie, object) keyed with of all $('#country :selected').val() values and mapped to 1) functions, or 2) objects with fields suitable for the operation you want to perform for each?

Then you could do

var lookup = { 'p1': function() { $("fieldset#state").hide().load('pl.txt').fadeIn(800); } }
//or
var lookup = { 'p1': { selector: 'p1.txt', speed: 800 } };

action = lookup[$('#country :selected').val()];

action();
//or 
$("fieldset#state").hide().load(action.selector).fadeIn(action.speed)
Scott Evernden
A: 

If the text files always have the same name as the value in your form element, then you could just make a variable like so

  $("#country").change(function () {
    var counrty = $('#country :selected').val();

        $("fieldset#state").hide().load(country+ '.txt').fadeIn(800);

    }
});
CEich
+5  A: 

You can do like this:

$("#country").change(function () {
  $("fieldset#state").hide().load($('#country :selected').val() + '.txt').fadeIn(800);
});

Edit:
For the list of available country, you can put them in an array, then do the search

$("#country").change(function () {
    var supportCountries = ['pl',]; //put more here
    var country = $('#country :selected').val();
    if(supportCountries.indexOf(country))
        $("fieldset#state").hide().load(country + '.txt').fadeIn(800);
    else
        $("fieldset#state").hide().load('default.txt').fadeIn(800); //here is load the default text, change if you has another way.
});

For more detail, if you want to replace switch, then let's use for/loop to find the matching case, then execute the action for that case.

Bang Dao
No need to run the selector again since `this` refers to the `<select>`. Just do `$(this).val()` or `this.value` (if the options have a value attribute).
patrick dw
@patrick dw: actually, we don't know if `$('#country :selected')` and `$("#country")` are one object or not, so I keep using `$('#country :selected')` for Tom easier to understand.
Bang Dao
Awesome! Thanks, I have to keep learning and practicing a lot! Thanks!
Tom
@Bang Dao - What do you mean? There can only be one `#country` element on the page. And doing `$(this).val()` *gives* you the value of selected option.
patrick dw