Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a text area which users can populate using buttons however, clicking the second replaces the value of the first button. I am trying to collect all the values of the buttons clicked prefereably indeneted one after another or spaced out then submitted in a form. Any ideas?

<form action="mailto:blahblah.com Feedback" method="post" enctype="text/plain">
			Email Adress:<br />
		<input type="text" id ="fav" name="email_address" value="" maxlength="100" /><br />

		<textarea id="text"></textarea>
		
		<div class="ui-btn ui-btn-inline" id="one">Red Wine</div>
		<div class="ui-btn ui-btn-inline" id="two">Item 2</div>
		<div class="ui-btn ui-btn-inline" id="three">Item 3</div>


		<input type="submit" value="Submit" />

		</form>



<script>
	$(document).ready(function(){
	
    $("#one").click(function () {
	$('#text').val("Red Wine")
	});
	
	$("#two").click(function () {
	$('#text').val("White Wine")
	});

});
	</script>


What I have tried:

<pre><script>
	$(document).ready(function(){
	
   if($("#one").click(function ()) {
	$('#text').val("Red Wine")
	});
	
	if($("#two").click(function ()) {
	$('#text').val("White Wine")
	});

});
	</script>
Posted
Updated 24-Dec-20 5:35am
Comments
BabyYoda 23-Dec-20 16:22pm    
I think you misunderstand what a click event is. A click event is when they click the button. So, do not IF they click the button. When they click the button set the text.
Member 15025314 23-Dec-20 16:28pm    
i'm not following by do not if?
BabyYoda 30-Dec-20 21:00pm    
Get rid of the if.

1 solution

I'll give it to you as javaScript (I don't us jQuery). This pattern, however, is complimented the same in virtually any language.
JavaScript
function yourOnClickEventHandler(some_input_reference) {

  var anObject = document.getElementById('text'); // the id from your code
  anObject.value = anObject.value+"\n"+some_input_reference;

} //
Now the argument to the function, some_input_reference, is a bit vague. I happened to use it as though it were your added additional text BUT it may be, instead, a reference to something else.

For example, if your button objects had values then you could, for example, send the button's id as reference and used it as the argument for
JavaScript
document.getElementById(some_input_reference);
or, you may wish to us the object, itself "this" as an argument and then you can use
JavaScript
some_input_reference.value
and append it.

Lot's of routes, but the basic pattern is add/append/concatenate the new value to the current content of your textarea.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900