Jump to content

jQuery help


jvriends

Recommended Posts

Thread http://forum.whmcs.com/showthread.php?t=28740 demonstrates how to change the state textbox to a dropdown menu. This is very important to me as a client in my country could enter in an incorrect state, and not be charged tax. Many users have worked from the origional post and made it so it would also display the texbot with the current value on the clientdetails.tpl template.

 

The issue here, is again, since it is a textbox, clients could enter incorrect data. I have modified the code so that it remains a dropdown and selects the state. My code is a bit messy as it is a bunch of if statements. Is there a better way?

 

var ar = new Array();

ar['CA'] = "<option>Alberta</option><option>British Columbia</option><option>Manitoba</option><option>New Brunswick</option><option>Newfoundland and Labrador</option><option>Northwest Territories</option><option>Nova Scotia</option><option>Nunavut</option><option>Ontario</option><option>Prince Edward Island</option><option>Quebec</option><option>Saskatchewan</option><option>Yukon Territory</option>";

jQuery(document).ready(function(){
jQuery("#country").change(function() {
	var country = jQuery(this).val();
	if (ar[country]) {
		jQuery("#state").hide();
		if (ar[country].indexOf('=')==-1) {
			jQuery("#statedropdown").html('<select id="statedd"><option>'+ar[country].replace(/\174/g,"</option><option>")+'</option></select>');
		}else{
			jQuery("#statedropdown").html('<select id="statedd"><option value="'+ar[country].replace(/\075/g, '">').replace(/\174/g,'</option><option value="')+'</option></select>');
		}
		jQuery("#state").val(jQuery("#statedd").val());
	} else {
		jQuery("#state").val('');
		jQuery("#state").show();
		jQuery("#statedropdown").html('');
	}
});

jQuery("#statedd").livequery("change", function(event) {
		jQuery("#state").val(jQuery(this).val());
});

var country = jQuery("#country").val();
var state = jQuery("#state").val();
if (ar[country]) {
	jQuery("#state").hide();
	if (ar[country].indexOf('=')==-1) {
		jQuery("#statedropdown").html('<select id="statedd"><option>'+ar[country].replace(/\174/g,"</option><option>")+'</option></select>');

		if (jQuery("#state").val() == 'Alberta') {
			$("#statedropdown option:contains('Alberta')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'British Columbia') {
			$("#statedropdown option:contains('British Columbia')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Manitoba') {
			$("#statedropdown option:contains('Manitoba')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'New Brunswick') {
			$("#statedropdown option:contains('New Brunswick')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Newfoundland and Labrador') {
			$("#statedropdown option:contains('Newfoundland and Labrador')").attr("selected","selected");
		}			

		if (jQuery("#state").val() == 'Northwest Territories') {
			$("#statedropdown option:contains('Northwest Territories')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Nova Scotia') {
			$("#statedropdown option:contains('Nova Scotia')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Nunavut') {
			$("#statedropdown option:contains('Nunavut')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Ontario') {
			$("#statedropdown option:contains('Ontario')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Prince Edward Island') {
			$("#statedropdown option:contains('Prince Edward Island')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Quebec') {
			$("#statedropdown option:contains('Quebec')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Saskatchewan') {
			$("#statedropdown option:contains('Saskatchewan')").attr("selected","selected");
		}

		if (jQuery("#state").val() == 'Yukon Territory') {
			$("#statedropdown option:contains('Yukon Territory')").attr("selected","selected");
		}

	}else{
		if (state=='') {
			jQuery("#statedropdown").html('<select id="statedd"><option value="'+ar[country].replace(/\075/g, '">').replace(/\174/g,'</option><option value="')+'</option></select>');
		}else{
			states = '<option value="'+ar[country].replace(/\075/g, '">').replace(/\174/g,'</option><option value="')+'</option>';
			jQuery("#statedropdown").html('<select id="statedd">'+states.replace(state+'"', state+'" selected="selected"')+'<option>dd</option></select>');
		}
	}
	jQuery("#state").val(jQuery("#statedd").val());
} else {
	jQuery("#state").show();
	jQuery("#statedropdown").html('');
}
});

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated