Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Attach Event to all the TextBoxes in a WebPage

24 Apr 2014 1  
Inspired by a question, I am blogging about a simple technique to attach KeyPress Event to all the TextBoxes of a WebPage.

 

<img alt="All TextBoxes except Email type allow only numbers" class="size-full wp-image-261" src="http://taditdash.files.wordpress.com/2014/04/all-textboxes-except-email-type-allow-only-numbers.jpg?w=800" style="height: 463px; width: 500px;" />

All TextBoxes except Email type allow only numbers

Updated on 25 April 2014

Blog Continues...

Inspired by a question, I am blogging about a simple technique to attach KeyPress Event to all the TextBoxes of a WebPage.

And What is the Technique?

  • Loop through all the Controls in the WebPage
  • Find which are TextBoxes
  • For those boxes, attach KeyPress Event

We will do it using JavaScript.

Can You Elaborate Each Step?

Why not !!! Let’s explore.

Loop Through All the Controls in the WebPage

var allInputs = new Array();

// store collection of all <input/> elements
allInputs = document.getElementsByTagName('input');

So, allInputs array would contain all the input elements on the WebPage.

Find Which are TextBoxes

for (i = 0; i < allInputs.length; i++) {
    // loop through and find <input type="text"/>
    if (allInputs[i].type == 'text') {
        // This is a TextBox. Here we can attach any Event.
    }
}

Here, we are looping through all the inputs and checking if its type is text. If satisfied, it is a TextBox.
In that if clause, we can attach any Event we need. We will attach the KeyPress Event in the next step.

For those Boxes, Attach KeyPress Event

We will attach KeyPress Event as an example. For that, we will use jQuery KeyPress.

for (i = 0; i < allInputs.length; i++) {
    // loop through and find <input type="text"/>
    if (allInputs[i].type == 'text') {
        // This is a TextBox. Here we can attach any Event.
        // Let's attach KeyPress Event to the TextBox.
        $(allInputs[i]).keypress(function (event) {
            if (!isNumber(event)) {
                event.preventDefault();
            }
        });
    }
}

Here, we attached the KeyPress Event and inside that Event, we are calling one function isNumber(event). If the pressed key is not a number, it would ignore as event.preventDefault() comes into action, else it will allow.

Note

The function isNumber(event) just checks if the pressed key is a numeric key or not. Refer to the demo link given below to see the code for this function.

See This In Action

See demo here.

Update, Improvements and Quicker jQuery Approach

Thanks to Kshirodra Meher and Subhajit Datta for the valuable suggestions.

What They Suggested?

In our first step, we looped through all the TextBoxes present in the Page, which can be optimized with a simpler jQuery block coded below.

$("input[type=text]").bind("keypress", function (e) {
    if (!isNumber(e)) {
        return false;
    }
});

So, the main game changer here is the Selector $("input[type=text]"). It selects all the inputs of type text. To know more, click Attribute Equals Selector [name="value"]. We have also restricted the paste event on the TextBoxes because it was allowing all characters into the box.

$("input[type=text]").bind("paste", function (e) {
    return false;
});

New Demo

Check out the new demo here.

Share Your Thoughts !!!

If you like the blog, share among your friends. Feel free to comment if you have any doubts or queries.

<img border="0" src="http://feeds.wordpress.com/1.0/comments/taditdash.wordpress.com/250/" /> <img border="0" height="1" src="http://stats.wordpress.com/b.gif?host=taditdash.wordpress.com&blog=60876978&post=250&subd=taditdash&ref=&feed=1" width="1" />

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here