Mik_A Mik_A - 5 months ago 29
Javascript Question

Add multiple val to input from class

I have many div elements with same class names. I want the user to be able to choose some of them in to a form input text element (value). So far I only made it put the last clicked one.

<div class="post">
<div class="addThis">A</div>
<div class="addThis">B</div>
<div class="addThis">C</div>
</div>
<input type="text" id="Choice">

jQuery('.addThis').click(function() {
var x = jQuery(this).closest('.post').find('addThis').text();
var y = jQuery("#Choice").val(x);


The var y is there because I tried to do something with it. But all attempts failed. Tried with

var t += y;
jQuery("#Choice").val(t);


and a lot that I already forgot

Thanks for your help

Answer

Is this what your trying to do:

$('.addThis').click(function() {
    var x = $(this).html();
    $("#Choice").val($("#Choice").val() + x);
});
Comments