The Mighty Blog

Feb11

Written by:Will
2/11/2009  RssIcon

Since DotNetNuke adopted jQuery into the DNN core, I have been using it. Here is another tip for a common scenario.

The folks working on jQuery have really done a fantastic job of making the lives of developers across the globe earier.  But they do not need me to tell them that.  :)

I am so happy that the DotNetNuke Core Team decided to put jQuery support into the DNN Core.  That has simplified and decreased UI development time for me exponentially.  And it has made UI development more fun too!  Moving on...

Here is the scenario that I ran into today.  A website visitor enters a value into a textbox, or selects a value from an autocomplete implementation.  No matter how much text is in the textbox, the novice web form user will not know how to quickly replace that text.  They will simply click once in the textbox, and then use many keystroke of both the <Delete> and <Backspace> keys to remove the text before they enter in new text.  This is not a favorable usability experience.

Here is how you might fix that: make the textbox automatically highlight or select all of the text in the textbox when the visitor clicks in it.  The main problem has always been getting things like this to work in all browsers.  Here is how you would implement this using jQuery.

Let's assume that we have the following text input:

<input type="text" id="txtInput" />

In most cases, we would want to have this feature available to all textboxes across our website, so we would create a function to handle this, and call it as needed.  Calling this function with the expected argument will make execute the highlighting of the text.

function selectAllText(textbox) {
    textbox.focus();
    textbox.select();
}

Assuming you are developing against DotNetNuke 5, or have jQuery already imported into your website, add the following jQuery to your site for each textbox.  (If you have a lot of textboxes, we could do this differently, but that's for a different post.)

jQuery('#txtInput').click(function() { selectAllText(jQuery(this)) });

Now, when you run the code, you will end up with a working textbox, like the example below.

Technorati Tags: , , , ,

Copyright ©2009 Will Strohl

8 comment(s) so far...


Gravatar

Why not apply this functionality application wide?
jQuery(':text').click(function() {
textbox.focus();
textbox.select();
});
Do you see think the function selectAllText(textbox) could be reused somewhere?

By Dejan on   2/12/2009
Gravatar

Yes. That is the statement made preceding the jQuery example.

In many cases, we wouldn't want every single textbox on the website to have this feature enabled. This is especially true if the site is making use of any highly specialized 3rd party controls. Their functionality would break. This example assumes that you do indeed want to reuse the function for specific textbox inputs.

By Will on   2/12/2009
Gravatar

it seems the code is broken in internet explorer 6.0

By razvan on   12/30/2009
Gravatar

That's interesting, because the select() method is not specific to jQuery, as it is also a root method in JavaScript. I would suspect that there's something else preventing your select() method from executing properly.

By Will on   12/30/2009
Gravatar

Howzit Will,

I enjoy reading your posts. Great blog. Please could you assist me. I have been working with DNN for over a year now but just got into jquery recently. My jquery module works however how do i go about implementing it into DNN 5??? I really am struggling. Getting lost with some of the tutorials i have read. Some links would be great or if you could provide me with a step by step way to implement it.

Thanks very much.

By juan on   4/12/2010
Gravatar

Using jQuery in DNN modules is actually quite easy. Here is a blog post to get you started.

jQuery Tips for DotNetNuke Developers

By Will on   4/12/2010
Gravatar

Best to use focusin() instead of click(), so you can still select a part of the line you are editing in.

By Kriekkop on   6/1/2010
Gravatar

I haven't tried your suggestion, but mine works just fine in all popular browsers. Thanks for the tip though. :)

By Will on   6/1/2010

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Add to Technorati Favorites
Tweet about my blog
Will Strohl - The Mighty Blog - RSS Feed

Tag Cloud

Sort by:Tag | SizeRSS
The opinions expressed here are the personal opinions of Will Strohl and do not necessarily represent the views and opinions of the DotNetNuke Corporation.
© Copyright 2004-2010 by Will Strohl. All rights reserved.Website Skinned By: Ralph Williams  Website Hosted By: Applied Innovations