Can you tell me what this code says/does?

Can you please tell me what this code says and does?

$(document).on('change', '#category_id', function(event) {
	event.preventDefault();
	id = $(this).val();
	$('#sub_category_id').html(sub_categories_array["'"+id+"'"]);
});
$(document).on('change','#sub_categories_', function(event) {
	window.location.href = site_url+'/videos/category/<?php echo($_GET['id']) ?>/'+$('#sub_categories_').val();
});

I look forward to being enlightened

// Whenever someone changes the value of an element with the id of 'category_id' trigger this event...
$(document).on('change', '#category_id', function(event) {
	// Prevent the default action taking place (the changing of the value)
	event.preventDefault();
	// Instead, get the value of the element that is triggering the event (the category_id element)
	id = $(this).val();
	// Find the element with the id 'sub_category_id' and replace its contents with the value stored
	// in the sub_categories_array at the given ID (the value entered in the category_id element)
	$('#sub_category_id').html(sub_categories_array["'"+id+"'"]);
});

// When the element 'sub_categories_' changes, redirect the browser to the specified path
$(document).on('change','#sub_categories_', function(event) {
	// The path is based on the value of the ID parameter passed to the page and the value from the 'sub_categories_' element. 
	window.location.href = site_url+'/videos/category/<?php echo($_GET['id']) ?>/'+$('#sub_categories_').val();
});

In the future, you can throw in some of your own console.log and use dev tools to figure out what a piece of code does.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.