Jump to content

Syntax check please?


chiprivers

Recommended Posts

Could somebody please check my syntax in the below javascript function?  I am not overly experienced with complex javascript so I have thrown this together based mainly on my knowledge of php.  I am pretty sure the logic behind my function is right but I am unsure of the syntax.  It is throwing up an error whenever I call this function. 

 

function selecter(click_ff,click_start,click_end,res) {

//if (document.getElementById(su_ff).value == null) {

	// set form field values
	document.getElementById(su_ff).value = click_ff;
	document.getElementById(su_start).value = click_start;
	document.getElementById(su_end).value = click_end;


} else if (document.getElementById(su_end).value - document.getElementById(su_start).value == res + 1) {

	// unshade previously selected cells


	// set form field values
	document.getElementById(su_ff).value = click_ff;
	document.getElementById(su_start).value = click_start;
	document.getElementById(su_end).value = click_end;


} else if (click_ff != document.getElementById(su_ff).value) {

	// unshade the previously selected cell/s


	// set form field values
	document.getElementById(su_ff).value = click_ff;
	document.getElementById(su_start).value = click_start;
	document.getElementById(su_end).value = click_end;

} else {

	if (click_start < document.getElementById(su_start).value) {

		// set form field values
		document.getElementById(su_start).value = click_start;

	}

	if (click_end > document.getElementById(su_end).value) {

		// ser form field values
		document.getElementById(su_end).value = click_end;

	}

}

// shade selected cells
document.getElementById('_'+click_ff+'_'+click_start+'_'+click_end).style.backgroundColor = '#333333';

}

Link to comment
Share on other sites

The only errors I see have nothing to do with JS specifically:

 

1. You commented out the first IF condition so the closing } before the first ELSE IF is out of context and plus you can't have ELSE IF statements without an IF statement.

2. The function is full of variables that are not defined - unless you defined them as gloabl variables somewhere else. The parameters in the function definition are named "click_???" but in the code it is "su_???"

Link to comment
Share on other sites

Many thanks mjdamato.

 

The commented out first line was an error!  I was commenting out various bits for testing to try and find the problem and obviously left the comments there when I copied it over to the thread.

 

The variables used are all valid in the context of where the function is being used.  However, I have found the problem to have been where I using the getElementById() I needed to put the ids in ' ', ie getElementById('su_ff).

 

That part of it is working fine now :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.