Reputation: 3
Does anyone know how can I insert values from the drop-down list into textarea, when it is being selected? I went searching around the internet and I tried doing it, yet it does not appear at all.
Below is the code snippets of what I have done but I'm stuck where I selected the value from drop-down list, it doesn't appear in my textarea at all. I need help.
Thanks people.
Variable.php
<?php
$variable_arr = array("Mobile", "Name", "Amount", "Due", "Etc");
$variable_str = '<select name="Variable" id="drp_dwn">';
$variable_str .= '<option selected><Select Data></option>';
foreach($variable_arr as $variable)
{
$variable_str .= '<option value="'. $variable .'"><'.$variable.'></option>';
}
$variable_str .= '</select>';
?>
change.js
$(document).ready(function() {
$("#drp_dwn").change(function () {
var str = "";
$("selected").each(function () {
str += $(this).text() + " ";
});
$("textArea").text(str);
}).change();
});
index.php
<textarea rows="4" cols="40" type="text" name="content" id="textArea"></textarea>
<label> <input type="submit" value="Send" name="submit" id="send_box"> </label>
Upvotes: 0
Views: 1488
Reputation: 663
Try this
$(document).ready(function(){
//adding event listener
$('#drp_dwn').on('change', function(){
//assigning the selected option to a variable
var str = $(this).val();
//so sending value to textbox
$('#textArea').text(str);
});
});
Upvotes: 0
Reputation: 9637
use val() method instead of text()
$("#drp_dwn").change(function () {
$("#textArea").val(this.value);
}).change();
Upvotes: 1