Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Populate Textarea based on Select

  1. #1
    Join Date
    Jan 2008
    Posts
    51
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default Populate Textarea based on Select

    Hi..

    I'm looking for some javascript that will populate a <textarea> element based on what is selected in a <select> element within the same form.

    If anyone could help me out, that would be amazing!

    Thanks

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    There are a few, yes, but to help can you be more specific in the application uses for this?

    Are there set responses, like if I select "first choice" from the drop down will the text box show "first choice" or will it show something different?

    etc.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Jan 2008
    Posts
    51
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Well, ideally, the idea is this:

    From a <select> element, I would select a certain topic/keyword. Then, the keyword would appear in a <input type='text'> element and a cooresponding block of text would be added to the <textarea>.

    These would all be preset. That is to say, they're not being created dynamically or anything so I can hard code them.

    The more important of the two events happening, is the textarea being populated with a pretermined string associated with the keyword. But I'd love to be able to do both.

    So to answer your question, I'm looking to do both.

    Copy the string from the <select> into the text <input>
    AND
    Copy a string associated with a partiular <option> into a <textarea>

    I hope that makes sense.

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Hi ReadyToLearn,
    Try these codes:
    HTML Code:
    <form name="form1">
    <select
    style="background-color: transparent; font-size: 10px; color: rgb(0, 102, 153); font-family: verdana;"
    name="menu">
    <option value="#">Quick Links ...</option>
    <option value="This is for value no.1">Value No. 1</option>
    <option value="This is for value no.2">Value No. 2</option>
    <option value="This is for value no.3">Value No. 3</option>
    <option value="This is for value no.4">Value No. 4</option>
    <option value="This is for value no.5">Value No. 5</option>
    <option value="This is for value no.6">Value No. 6</option>
    </select>
    <input
    style="font-size: 8pt; color: rgb(255, 255, 255); font-family: verdana; background-color: rgb(0, 102, 153);"
    value="Populate" type="button" onClick="document.form1.accept.value=document.form1.menu.options[document.form1.menu.selectedIndex].value;document.form1.textareaaccept.value=document.form1.menu.options[document.form1.menu.selectedIndex].value;">
    <br/>
    <br/>
    <input type="text" name="accept" style="width:200px;">
    <br/>
    <textarea size="100px" name="textareaaccept" style="width:200px;height:100px;">
    </textarea>
    </form>
    See if it helps
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Jan 2008
    Posts
    51
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Thank you!
    That'e exactly what I wanted.

    If it wouldn't be TOO much trouble, could someone point out how I could access the text of the <option> via javascript.

    My text of the <option> I mean the text in red below.
    <option>TEXT</option>.

  6. #6
    Join Date
    Jan 2008
    Posts
    51
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Nevermind, I figured it out!

    I changed

    Code:
    document.form1.accept.value=document.form1.menu.options[document.form1.menu.selectedIndex].value
    to
    Code:
    document.form1.accept.value=document.form1.menu.options[document.form1.menu.selectedIndex].text
    Works EXACLTLY like I wanted now.

    Thank you SO much, Rangana.

  7. #7
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You're welcome!..Glad I could help
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  8. #8
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Please help guys

    Please help..

    Code:
    <form name="form1">
    <select
    style="font-size: 10px; font-family: verdana;"
    name="menu">
    <option value="#">Quick Links ...</option>
    <option value="This is for value no.1">Value No. 1</option>
    <option value="This is for value no.2">Value No. 2</option>
    <option value="This is for value no.3">Value No. 3</option>
    </select>
    <input
    style="font-size: 8pt; font-family: verdana;"
    value="Populate" type="button" onClick="document.form1.textareaaccept.value=document.form1.menu.options[document.form1.menu.selectedIndex].text;">
    <br/>
    <textarea size="100px" name="textareaaccept" style="width:600px;height:400px;">
    </textarea>
    </form>
    I can't quite figure out why it is not loading the text from the option, but is instead loading the value of the option. Can anyone please help? I used the example given earlier on this thread but removed the text input because I wasn't sure what the purpose of it was.

    (am going to implement here: http://straycreations.com/admin/support_newticket when done).

  9. #9
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Just change the highlighted:
    Code:
    onClick="document.form1.textareaaccept.value=document.form1.menu.options[document.form1.menu.selectedIndex].text;
    ...with value
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  10. The Following User Says Thank You to rangana For This Useful Post:

    Ryya (05-03-2011)

  11. #10
    Join Date
    Dec 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I'm an idiot. Thank you.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •