Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I need to show divs on product page based on selected variant.
I dont want to use variant.id == 'variant name' because its a lot of products with sometimes 3x3 options.
I'm not that good on JS so i cant make it work with contains operator.
var selectCallback = function(variant, selector) {
if (variant) {
if (variant.title contains 'banana') {
$('.banana').css('display', 'block');
} else {
$('.banana').css('display', 'none');
}
}
Solved! Go to the solution
This is an accepted solution.
Try this:
var selectCallback = function(variant, selector) {
if (variant) {
var variantTitle = variant.title;
if (variantTitle.indexOf('banana') !== -1) {
$('.banana').css('display', 'block');
} else {
$('.banana').css('display', 'none');
}
}
This is an accepted solution.
Try this:
var selectCallback = function(variant, selector) {
if (variant) {
var variantTitle = variant.title;
if (variantTitle.indexOf('banana') !== -1) {
$('.banana').css('display', 'block');
} else {
$('.banana').css('display', 'none');
}
}
As Gina mentioned, in JS you can use indexOf or includes, e.g.: variantTitle.includes('banana').
You may want to sanitize first the title string. Take care of whitespace or lower/uppercase variations.
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024