ringocub
ringocub

Reputation: 457

Javascript Select not updating

I have the following code.

function validateStatuses(bill_id){
    var bill = bills[bill_id];
    var selects = $('#bill_'+bill.bill_id+' .status_select');
    var codes = $('#bill_'+bill.bill_id+' .status_code');
    var value = null;
    for (var i = 0; i < selects.length; i++) {
        //value = document.getElementById('#Item_'+Item.item_id+'statuses'+codes).value;
        value = selects[i].options[selects[i].selectedIndex].value;
        if (value == 'new'){
            for (var j = 0; j < codes.length; j++) {
            var blagh = codes[j].options[codes[j].selectedIndex].text;
                if( value == 'new' && blagh == "none"){
                    //A status exist of NEW with a status code of NONE this is not acceptable
                    $('#info_dialog').html('');
                    $('#info_dialog').append("<p>You are trying to process an object with a View of NEW and a This CODE of NONE. Please correct this issue before you proceed!</p><hr />");
                    $('#info_dialog').dialog({
                        buttons:{
                            Cancel: function(){
                                $(this).dialog('close');
                            }
                        }
                    });
                    return false;
                }//end if           
            }//end for
        }else{

        }//end if
    }//end for
    return true;
}//end function

I want the error to display only when the condition of value == 'new' && blagh == "none" is met. This works if the variable value is already set to something other than new. However, if the variable is new and I do not change the selected index of blagh the condition still fails. The condition is only accepted if I change the value of both value and blagh when value == new. The function is run from an onCLick event which I thought would force the value variable to update to the new selected index. What am I missing here? Any suggestions?

Upvotes: 0

Views: 207

Answers (1)

dgvid
dgvid

Reputation: 26633

You may need to run your function in response to the select element's change event, instead of the click event. The select control's value is not updated at the time you receive the click event.

Upvotes: 1

Related Questions