Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 21st, 2005, 09:26 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default change to proper case in javascript

Code:
// Function to change any text to Proper Case (e.g. picco -> Picco)
String.prototype.toProperCase = function() 
{
    return this.charAt(0).toUpperCase() + this.substring(1,this.length).toLowerCase();
}

          <input type="text" size=9 id="fname<%=a%>" name="fname<%=a%>" onkeyup="if (/'/.test(this.value)){this.value=this.value.replace(/'/g,'')}; " onblur="this.onkeyup();" class="font4">
how can i change the text in this textbox to proper case with this function when the focus is away from the textbox?

TIA>

Picco


www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old September 21st, 2005, 11:02 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Picco,

When you add a prototype function to an object, you can call it for any instance of that object.

As the value property of your field is a string, you can do this...
Code:
var fld = document.getElementById("myFieldId");
fld.value = fld.value.toProperCase();
HTH,

Chris

 
Old September 21st, 2005, 11:06 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

it tried onKeyPress="this.value.toProperCase()", but it didnt work....??

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt
 
Old September 21st, 2005, 11:22 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

That is because you are calling the function and not assigning it's return value to anything.
Code:
this.value = this.value.toProperCase()
 
Old September 22nd, 2005, 09:54 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

that worked excellent, thanks

www.crmpicco.co.uk
www.crmpicco.co.uk.tt
www.milklemonadechocolate.uk.tt
www.griswolds.uk.tt
www.piccosmini.co.uk.tt
www.morton.uk.tt





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to change the case of data in data list while nittin14 ASP.NET 1.0 and 1.1 Basics 3 April 24th, 2007 03:58 AM
Proper Case IN Xsl Israr XSLT 8 April 4th, 2006 01:39 PM
change to Proper Case in ASP crmpicco Classic ASP Basics 2 September 12th, 2005 10:33 AM
search string either Upper case or lower case rylemer Beginning VB 6 3 March 24th, 2004 04:23 PM
Check Case in a Case-Insensitive DB nbryson SQL Language 1 January 23rd, 2004 07:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.