Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert lowercase letter to upper case in javascript

Tags:

I have a code that will convert lower case letters to uppercase but it works only with IE and not in Crome or Firefox.

function ChangeToUpper()   {                      key = window.event.which || window.event.keyCode;              if ((key > 0x60) && (key < 0x7B))             window.event.keyCode = key-0x20;   }    <asp:TextBox ID="txtJobId" runat="server" MaxLength="10" onKeypress="ChangeToUpper();"></asp:TextBox> 

Even I tried with

document.getElementById("txtJobId").value=document.getElementById("txtJobId").value.toUpperCase();  

onBlur event of the textbox

What should I do to make it work in all browsers?

Thanks

like image 528
priyanka.bangalore Avatar asked Mar 01 '10 09:03

priyanka.bangalore


People also ask

How do I convert a lowercase string to uppercase?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.

How do you use lowercase and uppercase in JavaScript?

JavaScript provides two helpful functions for converting text to uppercase and lowercase. String. toLowerCase() converts a string to lowercase, and String. toUpperCase() converts a string to uppercase.

What is toUpperCase () an example of in JavaScript?

JavaScript's toUpperCase() method converts a string object into a new string consisting of the contents of the first string, with all alphabetic characters converted to be upper-case (i.e. capitalized). Note: JavaScript strings are immutable.

Is there a capitalize method in JavaScript?

The toUpperCase() method converts the string to uppercase.


1 Answers

<script type="text/javascript">     function ChangeCase(elem)     {         elem.value = elem.value.toUpperCase();     } </script> <input onblur="ChangeCase(this);" type="text" id="txt1" /> 

separate javascript from your HTML

window.onload = function(){         var textBx = document.getElementById ( "txt1" );          textBx.onblur = function() {             this.value = this.value.toUpperCase();         };     };  <asp:TextBox ID="txt1" runat="server"></asp:TextBox> 

If the textbox is inside a naming container then use something like this

var textBx = document.getElementById ("<%= txt1.ClientID %>");              textBx.onblur = function() {                 this.value = this.value.toUpperCase();             };) 
like image 171
rahul Avatar answered Oct 25 '22 13:10

rahul