<script src="//cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.2/select2.css">
<h4>After selecting type and garage, When the user will click on stock field, ther previously selected value will be put on the clicked stock, But my code gives me the same as first and second times</h4>
<br/>
<div id="items">
<p>Type:</p>
<select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
<p>Garage:</p>
<select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<p>Stock:</p><input type="text" name="stock[]" class="stock">
<p>Type:</p>
<select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
<p>Garage:</p>
<select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<p>Stock:</p><input type="text" name="stock[]" class="stock">
<p>Type:</p>
<select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option...
JavaScript
$(".select2").select2();
$('#items').on('click','.stock',function(){
var indx = $('.stock').index(this);
var p_id = $('.pd').eq(indx).select2("val");
var g_id = $('.gd').eq(indx).select2("val");
v = p_id+g_id;
$('.stock').eq(indx).val(v);
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.