Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't seem to bind two text inputs together

Tags:

html

jquery

bind

I have two form inputs that I need have matching field content. Meaning if I enter text in one field it is the exact same on the other field (they are in separate forms).

I thought I could use .bind() to do it but my script would not allow my text to bind to another input.

var inp = $("#text1");

if ("onpropertychange" in inp)
inp.attachEvent($.proxy(function () {
    if (event.propertyName == "value")
        $("div").text(this.value);
}, inp));
 else
  inp.addEventListener("input", function () { 
    $("#text2").text(this.value);
}, false);



<input type="text" id="text1" />
<input type="text" id="text2" />
like image 785
Sim Avatar asked Jan 18 '23 15:01

Sim


1 Answers

change keyup to change if you don`t want to edit it letter by letter; jsfiddle there

var $inputs = $('#input1, #input2');

$inputs.keyup(function(){
    $inputs.val($(this).val());
});
like image 98
mreq Avatar answered Jan 31 '23 03:01

mreq