i got the value of html radio button from javascript code and i want to assign that value to variable of shopify liquid code can i do that.
<span id="selected_variant_title">{{ selected_variant_title }}</span>
<script>
jQuery(document).ready(function() {
// Handle change event for radio buttons
$('input[type="radio"]').change(function() {
// Retrieve the selected value
var proteinValue = $('input[name="meal_radio_input"]:checked').val();
console.log("Test", $('input[name="meal_radio_input"]:checked').val());
});
});
var selected_variant_title = '';
function handleRadioChange() {
var radioButtons = document.getElementsByName('meal_radio_input');
for (var i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].checked) {
selected_variant_title = radioButtons[i].value;
console.log('Selected value: ' + selected_variant_title);
break;
}
}
// Update the content of the selected_variant_title span element
var selectedVariantTitleElement = document.getElementById('selected_variant_title');
selectedVariantTitleElement.textContent = selected_variant_title;
}
I want assign selected_variant_title in a liquid veriable like as
{% assign protein_option = selected_variant_title %}
2
Answers
As Liquid is a rendered side language you won’t be able to do that.
To the contrary you may assign Liquid value to JS vars.
You cant do that as liquid code runs first and Javascript next,so you need to use javascript in this case completely.