4

I have 2 textboxes and If I write anythng in textbox1 it should reflect immediately in textbox2 and If I write anything in textbox2 it should reflect in textbox1.

So how do I do that? I have seen this article before and how to Implement between two textboxes?

http://www.zurb.com/playground/jquery-text-change-custom-event

Here are my textboxes:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

4 Answers 4

5
$('#TextBox1').keydown(function(){
    $('#TextBox2').val($(this).val())
})
$('#TextBox2').keydown(function(){
    $('#TextBox1').val($(this).val())
})
Sign up to request clarification or add additional context in comments.

Comments

2
var $tbs = $("input[id^='TextBox']");
$tbs.keyup(function() {
    var that = this;
    $tbs.each(function() {
       $(this).val(that.value);
    }); 
});

Demo: http://jsfiddle.net/SGcEe/2/

Comments

1

The above way of referencing the textboxes won't work. ASP.NET generates some id for the server controls.

To have a relatively clean solution. I would suggest you add classes to the text boxes.

<asp:TextBox ID="TextBox1" CssClass="textbox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" CssClass="textbox2" runat="server"></asp:TextBox>

The jquery code will be as follows:

$('.textbox1').keyup(function() {
    $('.textbox2').val($(this).val());
});

$('.textbox2').keyup(function() {
    $('.textbox1').val($(this).val());
});

You can see an example using vanilla html at jsfiddle: http://jsfiddle.net/HUFGD/

1 Comment

Thank you neelesh and yes Asp.Net controls are server side and we need to change them as you said and I agree to that! and we can refer them by using <%=TextBox1.ClientID%>
0

this could be good trick

        $("input[id*='txtQty']").keyup(function (event) {
        var oldValue = event.target.defaultValue;
        var newValue = event.target.value;
        if (jQuery.trim(newValue) == "") {
            alert("Quantity can't be empty");
            $("input[id*='txtQty']").val(oldValue);
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.