jQuery, JSON, drop-down and dynamic HTML

JackTheKnife

Limp Gawd
Joined
Mar 27, 2006
Messages
272
I have try to update a div based on data from JSON (produced by Magento core) and choices from drop-down menu (onchange function reloadSKU())

Code:
   	jQuery(document).ready(function(){
	
	var json = <?php echo $this->getJsonSku() ?>;
	
	jQuery(".product-custom-option").change(function(){
			var sel_value = jQuery(".product-custom-option option:selected").val();
			console.log(sel_value);
			
			jQuery.each(json, function(i, val) {
			
			var item_opt = val;
						
       		jQuery.each(item_opt, function(key, valu) {
       			if (key=sel_value) {
					var sku = valu;
					console.log(sku);
				}
			});
			});
			
			jQuery(".p-sku").html("SKU: " + sku);
			
			
	})
	});
and JSON data:

Code:
    {"7":{"33":"807","32":"808","31":"809","30":"1015","29":"1021"}

Where "7" is the main product ID; "33","32","31" etc are options IDs from the drop-down (same as values) and "807", "808" etc are SKUs for particular ID.

I never was doing anything with JSON so simply idk why is not working :\

Thanks
 
Last edited:
OK, somehow I have solved it by code below:

Code:
	jQuery(document).ready(function(){
	
	var json = <?php echo $this->getJsonSku() ?>;
	
	var sku = 0;
	
	jQuery(".product-custom-option").change(function(){
			var sel_value = jQuery(".product-custom-option option:selected").val();
			console.log(sel_value);
			
			jQuery.each(json, function(i, val) {
			
			var item_opt = val;
						
       		jQuery.each(item_opt, function(key, valu) {
       			if (key==sel_value) {
					sku = valu;
					console.log(sku);
				}
			});
			});
			
			jQuery(".p-sku").html("SKU: " + sku);
			
			
	})
	});
 
Back
Top