trying to append value to textarea with select box

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Sep 2004
Posts: 7
Reputation: mwa is an unknown quantity at this point 
Solved Threads: 0
mwa mwa is offline Offline
Newbie Poster

trying to append value to textarea with select box

 
0
  #1
Jul 16th, 2008
I'm trying to create a tag like routine for music genres. I have an array with various music genres in it. Those genres appear in a select box so they can be added to a text area...

The problem is, I want to be able to add multiple genres instead of a new value replacing the old value.

Heres sample code of what I have so far ...

  1. <?php
  2. $tagarray = array ('Classical', 'Pop', 'Blues');
  3. ?>
  4.  
  5. Tags: <select name="tagmenu">
  6. <option value="#">Select Tags</option>
  7. <?php
  8. foreach($tagarray as &$value){
  9. echo '<option value="'.$value.'">'.$value.'</option>'."\n";
  10. }
  11. ?>
  12. </select>
  13. <input value="Add" type="button" onClick="document.form1.tags.value=document.form1.tagmenu.options[document.form1.tagmenu.selectedIndex].value;">
  14. <br />
  15. <textarea class="small" name="tags" rows="5" cols="40" wrap="virtual"></textarea>

Is there some subtle thing in "onCLick" I can change to get it to append the value?, ideally comma separated
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: jsrpatna is an unknown quantity at this point 
Solved Threads: 2
jsrpatna jsrpatna is offline Offline
Newbie Poster

Re: trying to append value to textarea with select box

 
0
  #2
Jul 16th, 2008
Hi,

You can rephrase your onclick event handling like this.

onClick="document.form1.tags.value +=document.form1.tagmenu.options[document.form1.tagmenu.selectedIndex].value;

Here, I'm assuming that your onclick functionality is working fine In the above case, the options are added inline to the previous selection. To add options one below another you should add the /n after the += operator. Idea is to save the value of the textarea before changing its value. If want to add comma then you can use the followings

onClick="document.form1.tags.value +=document.form1.tagmenu.options[document.form1.tagmenu.selectedIndex].value + ',';

You can modify this according to your needs so that the "," does not appear un-necessarily.
Another alternative is to provide your users the option of scrollable select with mulitiple select option. In that situation you will not require this button at all! To learn more about scrollable select which permits multiple selection you can refer the following link http://tutorialindia.com/html/html_select_element.php. Here you will need to use both of the select attributes, multiple as well as size.

Hope this helps!
Last edited by jsrpatna; Jul 16th, 2008 at 10:32 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7
Reputation: mwa is an unknown quantity at this point 
Solved Threads: 0
mwa mwa is offline Offline
Newbie Poster

Re: trying to append value to textarea with select box

 
0
  #3
Jul 16th, 2008
Awesome thanks a lot! I figured it was something simple.. I'm just a javascript noob.. I kept trying to append like you do in PHP, with periods.. which of course didn't work.

Thanks again !
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: geek_Brazil is an unknown quantity at this point 
Solved Threads: 0
geek_Brazil geek_Brazil is offline Offline
Newbie Poster

Re: trying to append value to textarea with select box

 
0
  #4
Oct 13th, 2008
I need to do a similar stuff, but I have no idea. Can u give some help or show me the way is your code working now? Appending from a mutiple select to the textarea box.

Thanks any way!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: trying to append value to textarea with select box

 
0
  #5
Nov 15th, 2008
Let's play a simple demo, hope you find this useful...

  1. <html>
  2. <head>
  3. <title><!--Sample--></title>
  1. <style type="text/css">
  2. @media handheld, print {
  3. body {
  4. width: 50%;
  5. height: 50%;
  6. font-size: 11px;
  7. background-color: #FFFFFF !important;
  8. }
  9. }
  10.  
  11. @media screen {
  12. body {
  13. background-color: #F6F6F6;
  14. color: #696969;
  15. font-size: 70%;
  16. font-family: Verdana,Helvetica, Arial, Sans-Serif;
  17. text-align: left;
  18. }
  19. }
  20. form {
  21. width: 250px;
  22. height: 150px;
  23. margin: 0;
  24. padding: 0;
  25.  
  26. }
  27. form label {
  28. float: left;
  29. clear: both;
  30. font-weight: 700;
  31. font-size: 12px;
  32.  
  33. }
  34. #selection {
  35. width: 100px;
  36. float: left;
  37. margin: 0;
  38. padding: 0;
  39. }
  40. #selection select {
  41. width: 150px;
  42. float: left;
  43. margin: 5px 0 25px;
  44. clear: right;
  45. }
  46. #listing {
  47. width: auto;
  48. float: left;
  49. height: auto;
  50. margin: 0 auto;
  51. }
  52. #listing textarea {
  53. float: left;
  54. padding: 0;
  55. width: auto;
  56. height: auto;
  57. color: #373832;
  58. margin: 10px 0;
  59. outline: 2px solid #C0C0C0;
  60. }
  61. </style>

  1. <script type="text/javascript">
  2. <!--
  3.  
  4. function listed()
  5. {
  6. frm1.txt.value += frm1.lst.options[frm1.lst.selectedIndex].value;
  7. if (frm1.txt.value.length >= 1) {
  8. for (var x = 0; x <= frm1.lst.options.length; x++) {
  9. frm1.lst.options[x].value = ', Item ' + x;
  10. }
  11. }
  12. }
  13. document.onclick = listed;
  14. //-->
  15. </script>
  1. </head>
  2. <body>
  3. <form name="frm1" action="javascript:void(0);" onsubmit="return false;">
  4. <div id="selection">
  5. <label> Categories: </label>
  6. <select name="lst" size="1">
  7. <option value="" disabled>Select An Item</option>
  8. <option value="Item 1">Item 1</option>
  9. <option value="Item 2">Item 2</option>
  10. <option value="Item 3">Item 3</option>
  11. <option value="Item 4">Item 4</option>
  12. <option value="Item 5">Item 5</option>
  13. <option value="Item 6">Item 6</option>
  14. </select></div>
  15. <div id="listing">
  16. <label> Post Request: </label>
  17. <textarea name="txt" rows="5" cols="40" wrap="virtual"></textarea></div>
  18. </form>
  19. </body>
  20. </html>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC