epiphany
epiphany

Reputation: 766

Jquery An Input value change according to another field

Right now, when one field change, the other field change accordingly. What i want to do is originally the 2nd field has some prefilled input from database, and when the 1st input field was fill in by the user, i want to keep the data from database plus adding the text input by the user after the database data. The problem currently is when new input was enter, the original data retrieved from the db disappear. Any solution?/ Many thanks

addEvent(document.getElementById('name'), 'keyup', function () {
   document.getElementById('url').value = this.value.replace(' ');
});


function addEvent(ele, evnt, funct) {
  if (ele.addEventListener) // W3C
    return ele.addEventListener(evnt,funct,false);
  else if (ele.attachEvent)  // IE
    return ele.attachEvent("on"+evnt,funct);
}
<form>
    <input type="text" id="name" />
    <input type="text" id="url" />
</form>

Upvotes: 0

Views: 56

Answers (2)

Amar Singh
Amar Singh

Reputation: 5622

Why dont you keep it simple ,since you have tagged it in jQuery ,here's my answer:

if(typeof set =="undefined"){
var set = $("#url").val();
}
$("#name").on('keyup',function(){

$("#url").val(set+$(this).val());


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form>
    <input type="text" id="name" />
    <input type="text" id="url" value="asdas"/>
</form>

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26298

Try this:

var oldValue = document.getElementById('url').value;
addEvent(document.getElementById('name'), 'keyup', function () {
   document.getElementById('url').value = oldValue + this.value.replace(' ');
});


function addEvent(ele, evnt, funct) {
  if (ele.addEventListener) // W3C
    return ele.addEventListener(evnt,funct,false);
  else if (ele.attachEvent)  // IE
    return ele.attachEvent("on"+evnt,funct);
}
<form>
    <input type="text" id="name" />
    <input type="text" id="url" value="www." />
</form>

here www. is already filled in second textbox, what ever you type in first texbox will append to the text in second textbox.

Upvotes: 2

Related Questions