Link to home
Create AccountLog in
Avatar of Daniel Wilson
Daniel WilsonFlag for United States of America

asked on

Javascript scope error

The following block of code is failing to refill the EntryDate box with the date after the
$(form).clearForm();

var entry_date attempts to be outside of either of those functions and therefore accessible.  But it doesn't appear to be. Do I need to make it global?  Outside of all the functions in the file?  Or is there a better way?  Thanks!

function setupAjaxForm(form_id, form_validations){
	var form = '#' + form_id;
	var form_message =  'server-message';
	
	// en/disable submit button
	var disableSubmit = function(val){
		$(form + ' input[type=submit]').attr('disabled', val);
	};
	
	// setup loading message
	$(form).ajaxSend(function(){
		//$('#server-message').removeClass().addClass('loading').html('Loading...').fadeIn();
	});
	
	// setup jQuery Plugin 'ajaxForm' 

    var entry_date;

	var options = {
		dataType:  'json',
		beforeSubmit: function(){
			if(!Validation()){
				return false;
			}
			

            /***** FETCH THE VALUE HERE ****/
            entry_date = $('#EntryDate').val();

			disableSubmit(true);
		},
		success: function(json){
			$.blockUI({ 
			message: json.message,  
					  css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: '.5', 
            color: '#fff' 
				} }); 
			setTimeout($.unblockUI, 2000); 
			
			disableSubmit(false);
			if(json.type='success')
				$(form).clearForm();
				$('#EntryDate').val(entry_date);
		}
	};
	$(form).ajaxForm(options);

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Daniel Wilson

ASKER

Wow, you're right on 47.

That ... doesn't keep 49 from executing though.  48 certainly executes.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Replying to your comment "That ... doesn't keep 49 from executing though.  48 certainly executes."
Actually it should.  If you're having "=" instead of "==", script hits an error and stops at line 47 and never proceed.