2

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

3
  • What are you trying to do with the text? Commented Jun 27, 2016 at 19:47
  • I try to get the text of the div class="addThis" to be added to a form input field. but only those that are clicked on. Now this code rewrites it everytime. thus adding only the last clicked text. Commented Jun 27, 2016 at 19:51
  • Have you thought about just using a series of checkboxes like <input type="checkbox" id="chkA" class="addThis" value="A" /><label for="chkA">A</label> Commented Jun 27, 2016 at 19:54

1 Answer 1

2

Is this what your trying to do:

$('.addThis').click(function() {
    var x = $(this).html();
    $("#Choice").val($("#Choice").val() + x);
});
Sign up to request clarification or add additional context in comments.

3 Comments

yes exactly!. thank you.. I will still see if I can make a element to be able added only once.
@Mik_A The right word you are looking for is concatenate
Sure. but I see it as a shopping basket, therefore added :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.