Avatar of Daniel Wilson
Daniel Wilson
Flag 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

JavaScriptjQuery

Avatar of undefined
Last Comment
Brian Tao

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brian Tao

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Daniel Wilson

ASKER
Wow, you're right on 47.

That ... doesn't keep 49 from executing though.  48 certainly executes.
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Robert Schutt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Brian Tao

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes