
Gabriel F. answered 10/26/19
Tutor
5.0
(85)
Full stack Javascript Engineer
Hey there. I think the best way to go about this is to comment what each line of code is doing:
// when there is a click on an element (likely a button)
// that has the classes '.button.btn-cart.mob_button_left'
jQuery('.button.btn-cart.mob_button_left').click(function() {
// if the element(s) that have the attribute 'product-options-block'
// (missing a "." if this is a class, or "#" if it is an id)
// doesn't have class 'product-options'
if (!(jQuery('product-options-block').hasClass("product-options"))) {
// disable the element(s) that have these classes:
// .button.btn-cart.mob_button.buttons'
jQuery('.button.btn-cart.mob_button.buttons').prop('disabled', true);
}
// if the condition in the "if" statement evaluates to false
else {
// check if the element whose id is 'select_label_size'
// has any HTML inside of it
if (document.getElementById('select_label_size').innerHTML) {
// if it does, disable the element(s) that have these classes:
// .button.btn-cart.mob_button.buttons
jQuery('.button.btn-cart.mob_button.buttons').prop('disabled', true);
}
// if element whose id is 'select_label_size'
// doesn't have any HTML inside of it
else {
// set 'disabled' to false (enable) the element(s)
// with these classes: .button.btn-cart.mob_button.buttons
jQuery('.button.btn-cart.mob_button.buttons').prop('disabled', false);
}
}
});
// this is a simplified version of the above
// creating variables makes the code easier to read in this case
// I named the variables according to the classes and id used
// so these may not be the best names. Someone more familiar with
// the project could come up with more semantic names but the ones
// chosen here do the job
var $mobButtonLeft = jQuery('.button.btn-cart.mob_button_left');
// need to add "." if 'product-options-block' is a class
var $productOptionsBlock = jQuery('product-options-block');
var $mobButtons = jQuery('.button.btn-cart.mob_button.buttons');
var $labelHasInnerHTML = document.getElementById('select_label_size').innerHTML;
$mobButtonLeft.click(function() {
if (!($productOptions.hasClass("product-options"))) {
$mobButtons.prop('disabled', true);
} else {
if ($labelHasInnerHTML) {
$mobButtons.prop('disabled', true);
} else {
$mobButtons.prop('disabled', false);
}
}
});
I hope this helps.