Sim
Sim

Reputation: 45

Can't seem to bind two text inputs together

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" />

Upvotes: 4

Views: 4321

Answers (3)

wkm
wkm

Reputation: 1762

What about this?

$('#input01').keyup(function() {
    value = $(this).val();
    $("#input02").val(value);
});

Upvotes: 0

mreq
mreq

Reputation: 6542

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());
});

Upvotes: 5

cpjolicoeur
cpjolicoeur

Reputation: 13106

$("#text1").change({
  $("#text2").val(this.val());
});

Upvotes: 4

Related Questions