views:

122

answers:

4

I am trying to find the best way to have a different function happen on change of my select box.

This is my select HTML

<select name="event-recurring">
<option selected="true">None</option>
<option value="daily">Daily</option>
<option value="weekly">Weekly</option>
<option value="monthly">Monthly</option>
</select>

My jquery is:

$('select[name=event-recurring] option:selected').change (function (){
//Will Do Something
});

How Do I tell it do something different on the different values?

Solution:

$('#event-recurring').change(function (){
    var $select = $(this);

    switch($select.val()) {
        case "daily":
           $('#recurring-options').remove();
           $("<div id='recurring-options' class='row'><label>Choose:</label>Choose a Date</div>").insertAfter('#recurring');
            break;
        case "weekly" :
        $('#recurring-options').remove();
              $("<div id='recurring-options' class='row'><label>Choose:</label>Weekly</div>").insertAfter('#recurring');
            break;
        case "monthly":
        $('#recurring-options').remove();
           $("<div id='recurring-options' class='row'><label>Choose:</label>Monthly</div>").insertAfter('#recurring');
            break;
        default:
            $('#recurring-options').remove();
            break;
    }    
});
A: 

Do you means do thing different on different selected value?

If so, I believe you can use 'this' object in the function to refer the source of the event (the select in this case) and you can check its value using '.val()' or '.text()' to see what the value that is and process accordingly.

Hope this helps.

NawaMan
+1  A: 

Pretty sure that this will work inside the change function:

$('select[name=event-recurring]').change (function (){
    switch ($('option:selected', this).val()) {
      case "daily":
        // do something
        break;
      case "weekly":
        // do something
        break;
      case "monthly":
        // do something
        break;
    }
});
travis
+3  A: 
$('select[name=event-recurring]').change (function (){
    var $select = $(this);

    switch($select.val()) {
        case "daily":
            // do something
            break;
        case "weekly" :
            // do something else
            break;
        default:
            // default case
            break;
    }    
});

I'd recommend giving the <select> an id too and use that to select it as it will be faster than by name

Russ Cam
Thank You, Perfect!
matthewb
No probs. One thing to bear in mind with using val() on a `<select>` to get the selected option is that it will return the value of the option if one is defined, otherwise it will return the text of the option.
Russ Cam
See my solution, is there are more elegant way to write a div and clear it out? I have to put more html into them but this is working?.
matthewb
@Russ, I believe that's how HTML is supposed to work - that is, it's not just some jQuery/javascript quirk.
nickf
@nickf - interesting, I'd not realised/noticed/clicked on that before :)
Russ Cam
@matthewb - have you seen .empty()? - http://docs.jquery.com/Manipulation/empty. I'd be inclined to keep the `<div id='recurring-options'>` there and simply amend the label in the change event handler
Russ Cam
A: 

Use a subquery on the selected value and get it's value attribute as the discriminator for your action:

$('select[name=event-recurring]').change( function() {
    var value = $(this).find(':selected').attr('value');
    if (!value) { // none
        do something
    }
    else if (value == 'daily') {
        do something else
    }
    ...
});
tvanfosson