Shopify themes, liquid, logos, and UX
Hello there,
We have been trying to fix an issue we have on our website for quite a long time and we need help from you now as we can't figure out how to solve this.
We have had the "How did you hear about us" box for quite a long time on our website now and the issue is that when a customer selects the "Other" option, there's an input text box that should appear automatically when "Other" is selected but which doesn't appear..
Here's the code below.
Thanks for your help, we very much appreciate it! ❤️
Mia
<div class="form-vertical">
<p>
<label id="how-did-you-hear-about-us--label" for="how-did-you-hear-about-us">COMMENT NOUS AVEZ-VOUS CONNU ?</label>
<span id="how-did-you-hear-about-us--error" style="display:none">{{ settings.hau_error_message }}</span>
<select required class="required"id="how-did-you-hear-about-us" name="attributes[how-did-you-hear-about-us]">
<option value=""{% if cart.attributes.how-did-you-hear-about-us == "" %} selected{% endif %}>Sélectionner une option...</option>
{% assign optionsArray = settings.hau_form_options | split: ',' %}
{% for o in optionsArray %}
{% assign option = o | strip %}
<option value="{{ option }}"{% if cart.attributes.how-did-you-hear-about-us == option %} selected{% endif %}>{{ option }}</option>
{% endfor %}
<option value="Other"{% if cart.attributes.how-did-you-hear-about-us == "Other" %} selected{% endif %}>Autre</option>
</select>
</p>
<p id="otherFormWrapper" style="{% unless cart.attributes.how-did-you-hear-about-us == 'Other' %} visibility:hidden{% endunless %}">
<label id="how-did-you-hear-about-us-other--label" for="how-did-you-hear-about-us-other">Autre</label>
<span id="how-did-you-hear-about-us-other--error" style="display:none">{{ settings.hau_error_message_other }}</span>
<input required class="required" id="how-did-you-hear-about-us-other" type="text" name="attributes[how-did-you-hear-about-us-other]" value="{{ cart.attributes.how-did-you-hear-about-us-other }}"/></p>
</div><script>
(function() {document.addEventListener("DOMContentLoaded", initForm);
document.addEventListener("shopify:section:load", initForm);function initForm(){
var formElement = document.querySelector('#how-did-you-hear-about-us');
var formError = document.querySelector('#how-did-you-hear-about-us--error');
var otherFormElement = document.querySelector('#how-did-you-hear-about-us-other');
var otherFormError = document.querySelector('#how-did-you-hear-about-us-other--error');
var otherFormWrapper = document.querySelector('#otherFormWrapper');
var checkoutButtons = document.querySelectorAll('[name="checkout"], [name="goto_pp"], [name="goto_gc"]');function showOrHideForm(){
if (formElement.value == 'Other'){
otherFormWrapper.style.display = '';
} else {
otherFormWrapper.style.display = 'none';
}
}function checkFormElement(){
if (formElement.value.length == 0){
formElement.classList.add('error');
formError.style.display = '';
} else {
if (formElement.classList.contains('error')){
formElement.classList.remove('error');
formError.style.display = 'none';
}
}
}function checkOtherFormElement(){
if (otherFormElement.value.length == 0){
otherFormElement.classList.add('error');
otherFormError.style.display = '';
} else {
if (otherFormElement.classList.contains('error')){
otherFormElement.classList.remove('error');
otherFormError.style.display = 'none';
}
}
}otherFormElement.addEventListener("input", function() {
{% if settings.hau_form_validation %}
checkOtherFormElement();
{% endif %}
});formElement.addEventListener("change", function() {
showOrHideForm();
{% if settings.hau_form_validation %}
checkFormElement();
{% endif %}
});checkoutButtons.forEach(function(element){
element.addEventListener("click", function(evt) {{% if settings.hau_form_validation %}
var validated = true;
if (formElement.value.length == 0){
checkFormElement();
validated = false;
}
if (formElement.value == 'Other'){
if (otherFormElement.value.length == 0){
checkOtherFormElement();
validated = false;
}
}
if (!validated) {
evt.preventDefault();
evt.stopPropagation();
}
{% endif %}
});
});}
})()
</script>
Please share your page URL!
Thanks!
Please share a screenshot or video of your problem!
Thanks!
Here is a video showing you the cart.
The "input text box" that should show below "Other" is not showing..
When I go to the next step and then step back, you can see the box in question which is now showing.
So it doesn't show in the cart but if the customers steps back to his cart, it then shows...
Hi @cocoa64
You just need to add the change event to your select dropdown. Please add the below script and let me know if it works.
var selectElement = document.getElementById("how-did-you-hear-about-us");
// Add an event listener to the select element.
selectElement.addEventListener("change", function() {
// Get the value of the selected option.
var selectedValue = selectElement.options[selectElement.selectedIndex].value;
console.log(selectedValue,"selectedValue")
// Check if the value is "other".
if (selectedValue === "Other") {
// Show the input field.
document.getElementById("otherFormWrapper").style.display = "block";
document.getElementById("otherFormWrapper").style.visibility = "visible";
} else {
// Hide the input field.
document.getElementById("otherFormWrapper").style.display = "none";
document.getElementById("otherFormWrapper").style.visibility = "hidden";
}
});
Thanks!
Sorry I'm not sure where exactly I should add this code?
Under the script section at the bottom?
I've tried adding it to different places but with no change
Just above the </script> tag
Which is the last line in your given code
Thanks, I've tried but it hasn't changed anything 😕
can u share the update code here?
not able to find my code in view source
Yes sure!
Sorry you couldn't see it as I had deleted it..
I've added it back now
<div class="form-vertical">
<p>
<label id="how-did-you-hear-about-us--label" for="how-did-you-hear-about-us">COMMENT NOUS AVEZ-VOUS CONNU ?</label>
<span id="how-did-you-hear-about-us--error" style="display:none">{{ settings.hau_error_message }}</span>
<select required class="required"id="how-did-you-hear-about-us" name="attributes[how-did-you-hear-about-us]">
<option value=""{% if cart.attributes.how-did-you-hear-about-us == "" %} selected{% endif %}>Sélectionner une option...</option>
{% assign optionsArray = settings.hau_form_options | split: ',' %}
{% for o in optionsArray %}
{% assign option = o | strip %}
<option value="{{ option }}"{% if cart.attributes.how-did-you-hear-about-us == option %} selected{% endif %}>{{ option }}</option>
{% endfor %}
<option value="Other"{% if cart.attributes.how-did-you-hear-about-us == "Other" %} selected{% endif %}>Autre</option>
</select>
</p>
<p id="otherFormWrapper" style="{% unless cart.attributes.how-did-you-hear-about-us == 'Other' %} visibility:hidden{% endunless %}">
<label id="how-did-you-hear-about-us-other--label" for="how-did-you-hear-about-us-other">Autre</label>
<span id="how-did-you-hear-about-us-other--error" style="display:none">{{ settings.hau_error_message_other }}</span>
<input required class="required" id="how-did-you-hear-about-us-other" type="text" name="attributes[how-did-you-hear-about-us-other]" value="{{ cart.attributes.how-did-you-hear-about-us-other }}"/>
</p>
</div>
<script>
(function() {
document.addEventListener("DOMContentLoaded", initForm);
document.addEventListener("shopify:section:load", initForm);
function initForm(){
var formElement = document.querySelector('#how-did-you-hear-about-us');
var formError = document.querySelector('#how-did-you-hear-about-us--error');
var otherFormElement = document.querySelector('#how-did-you-hear-about-us-other');
var otherFormError = document.querySelector('#how-did-you-hear-about-us-other--error');
var otherFormWrapper = document.querySelector('#otherFormWrapper');
var checkoutButtons = document.querySelectorAll('[name="checkout"], [name="goto_pp"], [name="goto_gc"]');
function showOrHideForm(){
if (formElement.value == 'Other'){
otherFormWrapper.style.display = '';
} else {
otherFormWrapper.style.display = 'none';
}
}
function checkFormElement(){
if (formElement.value.length == 0){
formElement.classList.add('error');
formError.style.display = '';
} else {
if (formElement.classList.contains('error')){
formElement.classList.remove('error');
formError.style.display = 'none';
}
}
}
function checkOtherFormElement(){
if (otherFormElement.value.length == 0){
otherFormElement.classList.add('error');
otherFormError.style.display = '';
} else {
if (otherFormElement.classList.contains('error')){
otherFormElement.classList.remove('error');
otherFormError.style.display = 'none';
}
}
}
otherFormElement.addEventListener("input", function() {
{% if settings.hau_form_validation %}
checkOtherFormElement();
{% endif %}
});
formElement.addEventListener("change", function() {
showOrHideForm();
{% if settings.hau_form_validation %}
checkFormElement();
{% endif %}
});
checkoutButtons.forEach(function(element){
element.addEventListener("click", function(evt) {
{% if settings.hau_form_validation %}
var validated = true;
if (formElement.value.length == 0){
checkFormElement();
validated = false;
}
if (formElement.value == 'Other'){
if (otherFormElement.value.length == 0){
checkOtherFormElement();
validated = false;
}
}
if (!validated) {
evt.preventDefault();
evt.stopPropagation();
}
{% endif %}
});
});
}
})()
var selectElement = document.getElementById("how-did-you-hear-about-us");
// Add an event listener to the select element.
selectElement.addEventListener("change", function() {
// Get the value of the selected option.
var selectedValue = selectElement.options[selectElement.selectedIndex].value;
console.log(selectedValue,"selectedValue")
// Check if the value is "other".
if (selectedValue === "Other") {
// Show the input field.
document.getElementById("otherFormWrapper").style.display = "block";
document.getElementById("otherFormWrapper").style.visibility = "visible";
} else {
// Hide the input field.
document.getElementById("otherFormWrapper").style.display = "none";
document.getElementById("otherFormWrapper").style.visibility = "hidden";
}
});
</script>
Seems an error coming on your theme is disturbing the script execution. Please refer the below screenshot.
I doubt your issue can't be resolved without having your store access.
I've checked with Judgeme app team about those errors and they have fixed them. They are not showing anymore but the script stilll isn't working properly..
What should I do to give you access to the store?
UP!
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024