Mahbub Mahbub - 2 months ago 22
Javascript Question

How to store multiple value from single input text and show it in Html.TextArea?

I want to add single value in textbox and after clicking the add this will show in TextArea. And when I will do this process in next time the new value will show in line after the last value. But in next time it is overwritting my last value and show the recent one. How can I save my last values and show them sequentially before the new value. Here is my html code,

<div class="row">
<div class="col-md-6">
<div class="input-group">
@using (Html.BeginForm("Process", "Home", FormMethod.Post, new
enctype = "multipart/form-data"
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">Connect To:</span>
@Html.TextBoxFor(model => model.ipAddress, new { @class = "form-control" })
<div class="col-md-6">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Recent Devices</h3>
@Html.TextArea("Recent Devices", (string)@ViewBag.recentDevices, new { style = "max-width:100%; min-height:250px", @class = "form-control" })

And in my controller code,

Session["recentDevices"] = con.ipAddress;
Viewbag.recentDevices = Session["recentDevices"];


If you want a simple Javascript solution, use:

document.getElementById("MyTextarea").value += "Added text";

Or, depending on how you coded it, you could also use:

var text = document.getElementById("YourInput").value
document.getElementById("MyTextarea").value += text;


I'm not sure if this helps you, but this script will put the value where the cursor is. The other scripts add the element to the end of the textarea, so they should work for you, but if you want a better script, use this one.

typeInTextarea(document.getElementById('MyTextarea'), "Added Text");

      function typeInTextarea(el, newText) {
    var start = el.selectionStart
    var end = el.selectionEnd
    var text = el.value
    var before = text.substring(0, start)
    var after  = text.substring(end, text.length)
    el.value = (before + newText + after)
    el.selectionStart = el.selectionEnd = start + newText.length