irfan harun irfan harun - 1 month ago 9
Javascript Question

grabbing multiple data from textbox into textbox using javascript

My code works well for a single-lined input into the textbox. For example:

name:adam


But what I want is, to grab the name and class when I input data into the textbox. Something like this:

Example:

name:adam
class:8th grade





<script>
function myFunction()
{
var x = document.getElementById("tarea");
document.getElementById("name").value = x.value.substring(5);
}
</script>


And in the HTML section, I have this:

<textarea rows="4" cols="50" id="tarea" onblur="myFunction()"></textarea>


name: <input type="text" id="name"></br>
class: <input type="text" id="class">

Answer

you should split your textarea based on newlines. something like

function myFunction()
{

var x = document.getElementById("tarea"),
    nameInput = document.getElementById('name'),
    classInput = document.getElementById('class');

var lines = x.value.split('\n');
var name = lines[0].substring(5);
var grade = lines[1].substring(6);

nameInput.value = name;
classInput.value = grade;   

}

fiddle: http://jsfiddle.net/MVWAD/