Question

This method responds to the OnChange method of a select element. When a user switches to text mode, I inform that user that what was typed will be lost. The user might decides to cancel the operation and keep whatever mode he/she was in (Text or Post).

function DisplaySMSElements() { 
        var goSMS = false;
        //  alert(previousAlertType); ==> IMPORTANT

        if ($('#User_AlertType').val() == '<%= (int) AlertType.SMS %>') {
            if(isRichTextMessage)
            {
                if($('#User_Message').val().length > 0)
                {
                    goSMS = confirm('Switching to text mode will 
                             result to data lost. Would you like to proceed?');
                }
                else {
                    goSMS = true;
                }

                if (goSMS) {
                    $('#User_Message').val('');
                    SMSCount = maxSMSCount;
                    isRichTextMessage = false;
                    disableTinyMCE();
                }
                else
                {                        
                    $('#User_AlertType').val(previousAlertType);
                }
            }
        }
        else
        {
            if (isRichTextMessage == false) {
                enableTinyMCE();
                isRichTextMessage = true;
            }
        }
        DisplaySMSCount();
    }

I have introduced a global variable called previousAlertType, which holds the value of the select element when this one gets the focus:

$(document).ready(function() {
   $('#User_AlertType').focus(function () {
      previousAlertType = this.value; 
   });
}

When the page loads, previousAlertType's value is Email, which is 1.

var previousAlertType = '<%= (int)AlertType.Email %>'; 

The problem is that it doesn't work unless I add the line where I display it using the alert (line where it's said IMPORTANT).

I added the alert to check the value of the previousAlertType. After I finished, I removed it and the methods stopped working. After researching the cause, I put it back then the method worked. I just don't understand the problem.

Thanks for helping

EDIT:

This is what triggers the above method

<select id="User_AlertType" name="User_AlertType" 
         onchange="javascript: DisplaySMSElements()">

         <option value="<%= (int)AlertType.Email %>">Email</option>
         <option value="<%= (int)AlertType.SMS %>">Text</option>
         <option value="<%= AlertType.Post %>">Post</option>
</select>
Was it helpful?

Solution

Your issue could be a number of different things. You have not posted a full set of code showing the failure. If you claim variable scoping issues then you need to show the scopes that your variables are defined if you expect anyone to find a solution.

The snippet of jQuery code you showed (doc:ready) is missing a closed parenthesis.

This fiddles just fine http://jsfiddle.net/P65B6/1/

var previousAlertType = '<%= (int)AlertType.Email %>'; 

$(document).ready(function() {
   $('#User_AlertType').focus(function () {
      previousAlertType = this.value; 
   }).change(DisplaySMSElements);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top