Ad

2 Forms With Jquery

- 1 answer

In the same page I have 2 forms, I want to be able to modify form 1 and modifications appears in form 2 Vice-Versa (Both ways).

It's working fine for the input field for the "quantity", but I have problems with the 2 Select menus.

As you will see I used OnPress for the quantity because onChange didn't work!

$("#Quantity").change(function() {
  $("#Quantity_2").val($(this).val());
});
$("#Quantity_2").change(function() {
  $("#Quantity").val($(this).val());
});

$("#SingleOptionSelector_0").change(function() {
  $("#SingleOptionSelector_0_2").val($(this).val());
});
$("#SingleOptionSelector_0_2").change(function() {
  $("#SingleOptionSelector_0").val($(this).val());
});

$("#SingleOptionSelector_1").change(function() {
  $("#SingleOptionSelector_1_2").val($(this).val());
});
$("#SingleOptionSelector_1_2").change(function() {
  $("#SingleOptionSelector_1").val($(this).val());
});

function updateQuantity_2(val) {
  $("#Quantity").val(val);
  $("#Quantity").trigger('change');
}

function updateQuantity(val) {
  $("#Quantity_2").val(val);
  $("#Quantity_2").trigger('change');
}

function updateSingleOptionSelector_0_2(val) {
  $("#SingleOptionSelector_0 option:selected").val(val);
  $("#SingleOptionSelector_0 option:selected").trigger('change');
}

function updateSingleOptionSelector_0(val) {
  $("#SingleOptionSelector_0_2 option:selected").val(val);
  $("#SingleOptionSelector_0_2 option:selected").trigger('change');
}

function updateSingleOptionSelector_1_2(val) {
  $("#SingleOptionSelector_1 option:selected").val(val);
  $("#SingleOptionSelector_1 option:selected").trigger('change');
}

function updateSingleOptionSelector_1(val) {
  $("#SingleOptionSelector_1_2 option:selected").val(val);
  $("#SingleOptionSelector_1_2 option:selected").trigger('change');
}
<form action="/cart/add" method="post" enctype="multipart/form-data" data-section="{{ section.id }}">
  <select onChange="updateSingleOptionSelector_0_2()" id="SingleOptionSelector_0" data-index="option{{ forloop.index }}">
    {% for value in option.values %}
      <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
    {% endfor %}
  </select>
  <select onChange="updateSingleOptionSelector_1_2()" id="SingleOptionSelector_1" data-index="option{{ forloop.index }}">
    {% for value in option.values %}
      <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
    {% endfor %}
  </select>
  <select name="id" id="ProductSelect-{{ section.id }}" data-section="{{ section.id }}" class="product-form__variants no-js">
    {% for variant in product.variants %}
      {% if variant.available %}
        <option {% if variant == product.selected_or_first_available_variant %} selected="selected" {% endif %} value="{{ variant.id }}">
          {{ variant.title }}
        </option>
      {% else %}
        <option disabled="disabled">{{ variant.title }} - {{ 'products.product.sold_out' | t }}</option>
      {% endif %}
    {% endfor %}
  </select>
  <label for="Quantity">{{ 'products.product.quantity' | t }}</label>
  <input type="number" id="Quantity_2" onPress="updateQuantity()" name="quantity" value="1" min="1" class="product-form__input" pattern="[0-9]*">
  <button type="submit" name="add" id="AddToCart-{{ section.id }}">ADD TO CART</button>
</form>

<form action="/cart/add" method="post" enctype="multipart/form-data" data-section="{{ section.id }}">
  <select onChange="updateSingleOptionSelector_0()" id="SingleOptionSelector_0_2" data-index="option{{ forloop.index }}">
    {% for value in option.values %}
      <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
    {% endfor %}
  </select>
  <select onChange="updateSingleOptionSelector_1()" id="SingleOptionSelector_1_2" data-index="option{{ forloop.index }}">
    {% for value in option.values %}
      <option value="{{ value | escape }}"{% if option.selected_value == value %} selected="selected"{% endif %}>{{ value }}</option>
    {% endfor %}
  </select>
  <select name="id" id="ProductSelect-{{ section.id }}" data-section="{{ section.id }}" class="product-form__variants no-js">
    {% for variant in product.variants %}
      {% if variant.available %}
        <option {% if variant == product.selected_or_first_available_variant %} selected="selected" {% endif %} value="{{ variant.id }}">
          {{ variant.title }}
        </option>
      {% else %}
        <option disabled="disabled">{{ variant.title }} - {{ 'products.product.sold_out' | t }}</option>
      {% endif %}
    {% endfor %}
  </select>
  <label for="Quantity">{{ 'products.product.quantity' | t }}</label>
  <input type="number" id="Quantity_2" onPress="updateQuantity()" name="quantity" value="1" min="1" class="product-form__input" pattern="[0-9]*">
  <button type="submit" name="add" id="AddToCart-{{ section.id }}">ADD TO CART</button>
</form>
Ad

Answer

First of all you call updateSingleOptionSelector_0_2() without a value and i think you throw an error there. This might be the reason other listeners doesn't work.

On the other hand change function works as expected.

$('#first').change(function(){
	$('#second').val($(this).val())
});
$('#second').change(function(){
	$('#first').val($(this).val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="first" class="select">
<option value="1">1 Option</option>
<option value="2">2 Option</option>
<option value="3">3 Option</option>
<option value="4">4 Option</option>
</select>

<select id="second" class="select">
<option value="1">1 Option</option>
<option value="2">2 Option</option>
<option value="3">3 Option</option>
<option value="4">4 Option</option>
</select>

Ad
source: stackoverflow.com
Ad