Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: multiselect click event doesn't respond to click

I have a multiselect box that contains a list of image urls. I am trying to enable a click event on the multiselect box: when a user clicks on a url, it should pass the url to a textarea with the id of "articleFullText". Below is my jquery code, however, it doesn't work and does not cause any error on JS console:

$('.multiselect').click(function() {
                    var src = $(this).val();
                    $('#articleFullText').val($('articleFullText').val() + src);
                });

My selectbox html:

<div class="controls">
                            <select name="images" class="multiselect" multiple="multiple">

                             <option value="http://localhost/images/1.jpg">http://localhost/images/1.jpg</option>
<option value="http://localhost/images/2.jpg">http://localhost/images/2.jpg</option>

                            </select>
                        </div>

textarea code:

<textarea class="form-control" rows="3" name="articleFullText" id="articleFullText></textarea>
like image 676
TonyGW Avatar asked Jan 28 '26 17:01

TonyGW


2 Answers

Two mistakes: http://jsfiddle.net/TrueBlueAussie/dGa97/1/

A missing # selector:

$('.multiselect').click(function () {
    var src = $(this).val() +"blah";
    $('#articleFullText').val($('#articleFullText').val() + src);
});

And a missing closing quote on id="articleFullText":

<textarea class="form-control" rows="3" name="articleFullText" id="articleFullText"></textarea>
like image 191
Gone Coding Avatar answered Jan 30 '26 08:01

Gone Coding


You didn't use the right event (change).

$('.multiselect').on('change', function() {
  $('#articleFullText').append($(this).val())
});

Here is a working fiddle : http://jsfiddle.net/9Ya27/1/

like image 25
Pierre Fraisse Avatar answered Jan 30 '26 09:01

Pierre Fraisse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!