Imrul.H
Imrul.H

Reputation: 5870

Select-box control with jquery

I am using a selectbox:

<select name="priority" id="priority">
    <option value="4">Low</option>
    <option value="3" selected="selected">Normal</option>
    <option value="2">High</option>
    <option value="1">Emergency</option>
</select>

When user select "Emergency", then a confirmation box opens and if user confirm, then "Emergency" is selected. Otherwise "Normal" should be selected. For This, I am using jquery:

$(document).ready(function() {
    $('#priority').change(function(){
        if($(this).val()=='1'){
            if(confirm("Are you sure this is emergency?")){
                return true;
            }
            else{
                //here some code is needed to select "Normal".
            }
        }
    });
});

Now, How can I do that?

Upvotes: 1

Views: 1040

Answers (3)

Nick Craver
Nick Craver

Reputation: 630399

Since you're just returning in the confirmation case, you can shorten your logic down to this:

$(function() {
  $('#priority').change(function(){
    if($(this).val()=='1' && !confirm("Are you sure this is emergency?")){
      $(this).val('3');
    }
  });
});​

You can give it a try here.

Upvotes: 1

Giles Smith
Giles Smith

Reputation: 1962

The following code snippet should do what you are after - it selects the priority select element by ID and then sets the selected value to 3.

else 
{
    $('#priority').val(3);
}

Upvotes: 0

dekomote
dekomote

Reputation: 4017

else{
     $(this).find("option[value=3]").attr("selected","selected");
     //and trigger change after that if you need it
     $(this).change();
    }

Upvotes: 0

Related Questions