Justinbernardus
Justinbernardus

Reputation: 129

change value of input by value of select on change

i need a simple jquery code.

i have a select with option and value. this value needs to replace the input value #msn_v on change.

<select class="outofstockselectswitch">
      <option value="1">
        Kies uw maat: 32
      </option>
      <option value="2">
       Kies uw maat: 36
      </option>
      <option selected value="3">
        Kies uw maat: 38
      </option>
 </select>



<form>
    <input id="msn_v" name="v" value="87371702">
</form>

so if pick 'Kies uw maat: 32' the value of #msn_v should change to 1 ? how do i do this?

Upvotes: 1

Views: 338

Answers (3)

HenryDev
HenryDev

Reputation: 4993

Here's a working solution. Hope it helps!

function getValue(selectObject) {
        var someValue = selectObject.value;
        document.getElementById("msn_v").value = someValue;
    }
<select class="outofstockselectswitch" onchange="getValue(this)">
    <option value="1">
        Kies uw maat: 32
    </option>
    <option value="2">
        Kies uw maat: 36
    </option>
    <option selected value="3">
        Kies uw maat: 38
    </option>
</select>



<form>
    <input id="msn_v" name="v" value="87371702">
</form>

Upvotes: 0

Mohamed Abbas
Mohamed Abbas

Reputation: 2298

VanillaJS solution: using onchnage event and value.

document
  .getElementById('outofstockselectswitch')
  .onchange = function () {
    document.getElementById('msn_v').value = this.value;
  }
<select class="outofstockselectswitch" id="outofstockselectswitch">
      <option value="1">
        Kies uw maat: 32
      </option>
      <option value="2">
       Kies uw maat: 36
      </option>
      <option selected value="3">
        Kies uw maat: 38
      </option>
 </select>



<form>
    <input id="msn_v" name="v" value="87371702">
</form>

Upvotes: 1

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48437

You can do this using jQuery val() method.

$('select').change(function(){
  $('#msn_v').val($(this).val());
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="outofstockselectswitch">
      <option value="1">
        Kies uw maat: 32
      </option>
      <option value="2">
       Kies uw maat: 36
      </option>
      <option selected value="3">
        Kies uw maat: 38
      </option>
 </select>
<form>
    <input id="msn_v" name="v" value="87371702">
</form>

Upvotes: 0

Related Questions