Henrique Van Klaveren
Henrique Van Klaveren

Reputation: 1760

How to copy a part of text, and populate inputs with Jquery

What is the best way to import data with Jquery or PHP.

I need read textarea, and identify blocks like this

name: this is name, age:this is age I need to get only parts between name: I NEED GET THIS age:

I dont have any idea how, and what I need use for make this. So I need one idea how i can begin?

The jquery selector, not important, becouse i can use any think like on change, or button click function, this is indiferent...

<textarea id='import'>
this is pasted text...
name: this is name 
age: this is age
other: have but i dont need
other2: have but i dont need
city: this is city
obs: this is obs </textarea>
<input type="text" id="name">
<input type="text" id="age">
<input type="text" id="city">
<input type="text" id="obs">
<button>Import</button>

Upvotes: 2

Views: 70

Answers (1)

Washington Guedes
Washington Guedes

Reputation: 4365

You can try this javascript code:

var input = document.getElementById("import");

input.addEventListener('input', function(){
  var content = input.value;
  
  content.replace(/(\S+): ?(.+)/g, function(m, id, value) {
    var element = document.getElementById(id);
    if (element) element.value = value;
  });
});
<textarea id="import"></textarea>
<br>name: <input type="text" id="name">
<br>age: <input type="text" id="age">
<br>city: <input type="text" id="city">
<br>obs: <input type="text" id="obs">

You can see the regex in action here.

Upvotes: 2

Related Questions