How can I use Jquery to read from a selected option with an array?
I have the PHP code below, I want to write a script to read a parameter value from a dropdown and put each value in the input field when the selection changes. Please, help.
<html>
<head>
<!-- Load jQuery from Google CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
//I want to write a script that will read the selected value and put in the input box
</script>
</head>
<body>
<?php
for ($i=0;$i<7;$i++)
{
?>
<select id='select$i'>
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<input type="text" name="name" id="inputoption$i" />
<?php }?>
<br />
</body>
</html>
+3
Ben2014
source
to share
6 answers
Try the following:
<html>
<head>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
</head>
<body>
<div class="category-panel">
<?php
for($i = 0; $i < 7; $i++)
{
?>
<select id='select<?php echo $i; ?>'>
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<input type="text" name="name" id="inputoption<?php echo $i; ?>"/>
<?php } ?>
<br/>
</div>
<script>
$("select").on("change", function()
{
$(this).next("input").val($(this).val());
});
</script>
</body>
</html>
+3
Rayon
source
to share
Better to change id to class:
$(function() {
$(document).on('change', '.selectMe', function() {
$(this).next().val($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class='selectMe'>
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<input type="text" name="name" class="inputoption" />
<select class='selectMe'>
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<input type="text" name="name" class="inputoption" />
+2
Norlihazmey Ghazali
source
to share
Create them as -
<?php
for ($i=0;$i<7;$i++)
{
?>
<select class="selectoption">
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<input type="text" name="name" class="inputoption" />
<?php }?>
With js -
$('.selectoption').on('change', function() {
$(this).next('.inputoption').val($(this).val());
});
DEMO
+1
Sougata Bose
source
to share
$("select[id^=select]").change(function(event){
$(event.target).next().val($(event.target).val());
});
The code works here:
https://jsfiddle.net/noxg9pnz/
+1
Cryptovirus
source
to share
You can try below script:
DEMO
$('.selc').on('change',function()
{
$(this).siblings('input[type=text]').val($(this).val());
});
0
Guruprasad rao
source
to share
I used Cryptovirus solution, I do it like a roar and it works really well.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
</head>
<body>
<?php
for ($i=0;$i<7;$i++)
{
echo("<script>
$('select[id^=select]').change(function(event){
$(event.target).next().val($(event.target).val());
});
</script>");
echo("<select id='select$i'>
<option value='1'>Option1</option>
<option value='2'>Option2</option>
<option value='3'>Option3</option>
</select>
<input type='text' name='name' id='inputoption$i' /> </br>");
}?>
</body>
</html>
0
Ben2014
source
to share