Mik_A Mik_A - 1 year ago 103
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>
<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;

and a lot that I already forgot

Thanks for your help

Answer Source

Is this what your trying to do:

$('.addThis').click(function() {
    var x = $(this).html();
    $("#Choice").val($("#Choice").val() + x);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download