We help IT Professionals succeed at work.

Need help compressing this code

blitzer109
blitzer109 asked
on
318 Views
Last Modified: 2012-05-08
Could somebody please help me compress this code down as I am sure it is to far to bulky at the moment.

You can see a working example here: http://www.i-cre8.com/jQuery/Dependent-Select-Lists/index.html

Thanks.
function makeSublist(parent,child,isSubselectOptional,childVal)
{
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));
	
		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
	
	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child).val(["'+ childVal +'"]).attr("selected","selected");
	
	// check values on page load
	$('#'+parent).ready( 
		function()
		{
			var parentValue = $('#'+parent).attr('value');
			$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
			if(isSubselectOptional) $('#'+child).prepend("<option value='none'> -- Select -- </option>");
			if ($('#'+child+' option').length == 0) $('#'+child).prepend("<option value=''> -- No Options Available -- </option>");
			if ($('#'+child+' option').length == 1 && $('#'+child+' option').val() == '') $('#'+child).empty().prepend("<option value=''> -- No Options Available -- </option>")
			$('#'+child).trigger("change");
		}
	);
	
	// check values on form element change
	$('#'+parent).change( 
		function()
		{
			var parentValue = $('#'+parent).attr('value');
			$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
			if(isSubselectOptional) $('#'+child).prepend("<option value='none'> -- Select -- </option>");
			if ($('#'+child+' option').length == 0) $('#'+child).prepend("<option value=''> -- No Options Available -- </option>");
			if ($('#'+child+' option').length == 1 && $('#'+child+' option').val() == '') $('#'+child).empty().prepend("<option value=''> -- No Options Available -- </option>")
			$('#'+child).trigger("change");
		}
	);
}

Open in new window

Comment
Watch Question

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('i w(4,2,l,9){$("v").u("<p t=\'x:n\' y=\'"+4+2+"\'></p>");$(\'#\'+4+2).b($("#"+2+" 3"));j 7=$(\'#\'+4).f(\'5\');$(\'#\'+2).b($("#"+4+2+" .h"+7).k());9=(A 9=="z")?"":9;$("#"+2).m(["\'+ 9 +\'"]).f("o","o");$(\'#\'+4).B(i(){j 7=$(\'#\'+4).f(\'5\');$(\'#\'+2).b($("#"+4+2+" .h"+7).k());6(l)$(\'#\'+2).8("<3 5=\'n\'> -- s -- </3>");6($(\'#\'+2+\' 3\').e==0)$(\'#\'+2).8("<3 5=\'\'> -- d a c -- </3>");6($(\'#\'+2+\' 3\').e==1&&$(\'#\'+2+\' 3\').m()==\'\')$(\'#\'+2).q().8("<3 5=\'\'> -- d a c -- </3>")$(\'#\'+2).r("g")});$(\'#\'+4).g(i(){j 7=$(\'#\'+4).f(\'5\');$(\'#\'+2).b($("#"+4+2+" .h"+7).k());6(l)$(\'#\'+2).8("<3 5=\'n\'> -- s -- </3>");6($(\'#\'+2+\' 3\').e==0)$(\'#\'+2).8("<3 5=\'\'> -- d a c -- </3>");6($(\'#\'+2+\' 3\').e==1&&$(\'#\'+2+\' 3\').m()==\'\')$(\'#\'+2).q().8("<3 5=\'\'> -- d a c -- </3>")$(\'#\'+2).r("g")})}',38,38,'||child|option|parent|value|if|parentValue|prepend|childVal|Options|html|Available|No|length|attr|change|sub_|function|var|clone|isSubselectOptional|val|none|selected|select|empty|trigger|Select|style|append|body|makeSublist|display|id|undefined|typeof|ready'.split('|'),0,{}))

Commented:
blitzer109,

What do you mean by compress?

Do you need simpler code, or do you need to remove all unnecessary white spaces?

Author

Commented:
Simpler Code, I have the same code in both

// check values on page load
        $('#'+parent).ready

and

// check values on form element change
        $('#'+parent).change

but when I try to combine them I cannot get it to work properly

thanks

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.