DGT
DGT

Reputation: 2654

Yes/No confirmation message

I'm trying to get an alert message with Yes/No option, where clicking on 'Yes' reloads a another drop-down option, and 'No' would revert back to the previous selection. Here is what I'm trying to say (obviously doesn't work):

<select size="1" name="m" id="m">  
    <option selected value="1">apple</option>
    <option value="2">ball</option>
    <option value="3">cat</option>
    <option value="4">dog</option>
    <option value="5">egg</option>
</select>

$('#m').change(function() {
    var answer = confirm("this will change your selection.");
    if (answer) { // ie, if i click 'OK'
        location.reload(); // reload the <select> option below to "selected"
    } else {
        break; // revert back to the previous selection
    }
});


<select size="1" name="a" id="a">  
    <option selected value="1">aaa</option>
    <option value="2">bbb</option>
    <option value="3">ccc</option>
</select>

Many thanks in advance. Feel free to click, and edit.

Upvotes: 1

Views: 1976

Answers (3)

David Conde
David Conde

Reputation: 4637

Try removing the

break;

I tried to run the code and Firebug halts the code when it gets to that part

Best regards,

Upvotes: 0

Yanick Rochon
Yanick Rochon

Reputation: 53546

I'm not too sure what you are trying to accomplish... is it this?

var _selected = $('#m').val();
$('#m').change(function() {
    var answer = confirm("this will change your selection.");
    if (answer) { // ie, if i click 'OK'
        _selected = $(this).val();
        $('#a').val(_selected);
    } else {
        $(this).val(_selected);
    }
});​

Upvotes: 2

alex
alex

Reputation: 490303

JavaScript's confirm() only displays OK and Cancel, not Yes or No.

Could you return false instead of break, because you are in a function?

Also, returning false does not cancel the change event. What you will need to do is store the previous selectedIndex, and if you have the confirm() return false, then set the selectedIndex back to the previous one.

Also, I hope you are wrapping that jQuery inside script elements and inside a $(document).ready().

Upvotes: 2

Related Questions