jake123 jake123 - 6 months ago 14
HTML Question

Paste the text in input box when clicked on drop down

I have a dropdown when I click on them I would like the text to paste in the input box having class = paste. This is what I have done so far

index.php

<select>
<option class="others">Option 1</option>
<option class="others">Option 2</option>
</select>
<input class="paste" disabled="disabled" autocomplete="off" type="text">


Jquery.js

$(document).on('click', '.others', function(e) {
e.preventDefault();
$('.paste').val($(this).text());
});


Here is a fiddle Fiddle

Answer

You need to use change event instead of click

<select id="someSelect">
   <option class="others">Option 1</option>
   <option class="others">Option 2</option>
 </select>

$(function() {
    $("#someSelect").change(function() {
        alert( $('option:selected', this).text() );
        $('.paste').val($('option:selected', this).text());
    });
});

For Demo

Comments