Help us fix an issue with "Hear about us.liquid"

Help us fix an issue with "Hear about us.liquid"

cocoa64
Excursionist
34 1 7

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>

Replies 13 (13)

dmwwebartisan
Shopify Partner
12321 2551 3726

@cocoa64 

Please share your page URL!

Thanks!

If helpful then please Like and Accept Solution | Email: dmw.webartisan@gmail.com |  Instagram: @dmw.webartisan
Check here PageFly App to customize your pages | #1 Product Filter & Search app on Shopify | The most powerful Shopify page builder app
cocoa64
Excursionist
34 1 7
dmwwebartisan
Shopify Partner
12321 2551 3726

@cocoa64 

Please share a screenshot or video of your problem!

Thanks!

If helpful then please Like and Accept Solution | Email: dmw.webartisan@gmail.com |  Instagram: @dmw.webartisan
Check here PageFly App to customize your pages | #1 Product Filter & Search app on Shopify | The most powerful Shopify page builder app
cocoa64
Excursionist
34 1 7

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...

gr_trading
Shopify Partner
1891 145 199

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";
  }
});

 

 

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
cocoa64
Excursionist
34 1 7

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

gr_trading
Shopify Partner
1891 145 199

Just above the </script> tag

 

Which is the last line in your given code

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
cocoa64
Excursionist
34 1 7

Thanks, I've tried but it hasn't changed anything 😕

gr_trading
Shopify Partner
1891 145 199

can u share the update code here?

 

not able to find my code in view source

 

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
cocoa64
Excursionist
34 1 7

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>

 

gr_trading
Shopify Partner
1891 145 199

Seems an error coming on your theme is disturbing the script execution. Please refer the below screenshot.

 

gr_trading_0-1687282987449.png

 

I doubt your issue can't be resolved without having your store access.

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee
cocoa64
Excursionist
34 1 7

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?

cocoa64
Excursionist
34 1 7

UP!