Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

JQuery: Synchronize Two ASP.NET Text Boxes

Rate me:
Please Sign up or sign in to vote.
4.90/5 (5 votes)
11 Oct 2014CPOL3 min read 13.3K   6   5
Learn how to synchronize two ASP.NET textboxes

jquery1

“The central concept behind jQuery is – find something, do something. More specifically, select DOM element(s) from an HTML document and then do something with them using jQuery methods. This is the big picture concept.” ___________________________Cody Lindley(jQuery Succinctly)

Introduction

As the title suggests, this post is about interacting with two ASP.NET text boxes. If any change/interaction happens to one of the text boxes, then we’ll try to do the same to the other text box, programmatically.

Designing the UI

Let’s start with a new WebForm. The UI in this case requires only two multiline text boxes separated by some white spaces. To keep things simple, we’ll concentrate on ‘how it works’ rather than ‘how it looks’. Now, the above quotation indicates that we need to find something and do something and thus it is a two step process. Here the ‘something’ is the text boxes and we can find them by their ids, class, tag, type, etc. Particularly for this example, we’ll employ the find by class trick. So, we need to mention the same CssClass for both the text boxes and the thing to notice is- this class may not exist at all.

ASP.NET
<div>
     <asp:TextBox runat="server" ID="TextBox1" CssClass="sync" 
      TextMode="MultiLine" Height="100px" />&amp;nbsp;&amp;nbsp;
     <asp:TextBox runat="server" ID="TextBox2" CssClass="sync" 
      TextMode="MultiLine" Height="100px" />
</div>

Writing the Code

In the script part, we’ll code for two things-

  • Synchronizing the text typed into one text box with the other
  • Synchronizing the scrolling in both the text boxes

So, we will write two functions for these two specific actions. The first operation can be done on keyup event and the second with scroll event.

JavaScript
<script>
        $(document).ready(function () {
            $(".sync").keyup(function () {
                $(".sync").val($(this).val());
            });
            $(".sync").scroll(function () {
                $(".sync").scrollTop($(this).scrollTop());
            });
        });
</script>

Result

As I have already said, we can demonstrate two operations:

  • Typing anything into one of the text boxes will result in displaying the same text in the other text box, simultaneously.
  • Type few lines of code so that the scroll bar is visible. Now when we scroll any of the text boxes using either keyboard or mouse, the other one will be scrolled automatically.

Explanation

We have embedded our code inside $(document).ready() function as this function is executed after the DOM is loaded and before the page contents are rendered into the browser. We can define a common function for both the textboxes with the use of CssClass name. The first action is defined inside the keyup event so that when we release the key, the action will be performed. The action here is nothing but copying the content of the current text box to the other. The val() with no argument returns the value of the text box and when some argument is passed to it, sets the argument as value of that text box. I hope this made the thing very clear and further explanation is not required. Now consider the second action. The scroll() event is fired when we try to scroll the content of text box either through key board or mouse. Again, scrollTop() with zero argument returns the current scroll position but, sets the scroll positions to a value which is the same as the argument if called with an argument.

Conclusion

Now we are able to develop a page with two text boxes which are synchronized upon typing text in to them or using the scroll bar. We can use more than two text boxes and it will still work as long as we use a common CssClass for them.

I hope you have enjoyed reading this post. Your feedback is very important to me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy vote of 5 Pin
Testing Dataweb1-Jan-15 2:43
Testing Dataweb1-Jan-15 2:43 
QuestionSome Limitations Pin
Mahmoud Al-Shareef14-Oct-14 22:11
Mahmoud Al-Shareef14-Oct-14 22:11 
GeneralMy vote of 5 Pin
Carsten V2.013-Oct-14 7:21
Carsten V2.013-Oct-14 7:21 
SuggestionKnockout Pin
Nejimon CR12-Oct-14 21:56
Nejimon CR12-Oct-14 21:56 
QuestionMy vote of 5 Pin
Prava-MFS12-Oct-14 3:41
professionalPrava-MFS12-Oct-14 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.