Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to covert lower case letter to uppercase and vice versa in javascript. my code is as follows:
XML
<html>
<head>
<title>validation</title>
<script language= javascript>
function strcon()
{
var a ;
 a = document.f1.t1.value;
for(i=0;i<a.length;i++)
{
if(a.charCodeAt(i) >= 65 && a.charCodeAt(i) <= 90)
 {
  alert(a.toLowerCase(i));
}
else
 alert(a.toUpperCase(i));
}
}
</script>
</head>
<body>
<form name=f1>
<input type=text name=t1><br>
<input type=button value=ok onclick="strcon()">


my problem is when i click on button i want simultaneous conversion of string case but getting string case conversion one by one .
Posted
Comments
SoMad 5-Jun-13 6:12am    
Are you saying that you see the characters displayed one by one in a popup box? That would be because you have the alert() inside the loop. Instead of displaying each character, put them in a string and display the entire string when the loop is completed.

Soren Madsen
Rambo_Raja 5-Jun-13 6:32am    
var a ;
a = document.f1.t1.value;
var kk;
for(i=0;i<a.length;i++)
{
if(a.charcodeat(i)>= 65 && a.charCodeAt(i) <= 90)
{
kk = (a.toLowerCase(i));
}
else
kk =(a.toUpperCase(i));
}
alert(kk);
}
like this?but it also not working.
SoMad 5-Jun-13 6:45am    
No, this way you reassign the variable every time you go through the loop. You need to add to it.
I am writing this on my phone, so I am afraid explaining it like this is the best I can do to help you.

Soren Madsen
Rambo_Raja 5-Jun-13 6:51am    
kk.thanks
SoMad 5-Jun-13 7:13am    
Sorry, but you are really close to having the solution.

Soren Madsen

JavaScript
<script language="javascript">
        function strcon() {
            var b = '';
            var a = "This Is A Sample String";
            for (i = 0; i < a.length; i++) {
                if (a.charCodeAt(i) >= 65 && a.charCodeAt(i) <= 90) {
                    b = b + a.charAt(i).toLowerCase();
                }
                else
                    b = b + a.charAt(i).toUpperCase();
            }
            alert(b);
        }
    </script>
 
Share this answer
 
v2
Comments
SoMad 5-Jun-13 6:41am    
No, as I understand it, he wants to 'flip' the cases.
Turn "A Test String"
into "a tEST sTRING"

Soren Madsen
Rambo_Raja 5-Jun-13 6:44am    
yes..exactly
Zafar Sultan 5-Jun-13 7:04am    
OK. Let me try. Will get back to you shortly.
Zafar Sultan 5-Jun-13 7:14am    
Check my updated solution.
SoMad 5-Jun-13 7:20am    
That looks like it will work :-)

Soren Madsen
<script language="javascript" type="text/javascript">

function checkCase(x,y) {
y = x.toUpperCase();
if ( x == y ) {
y = x.toLowerCase();
} return y;
}

function alterCase() {
var str = document.getElementById("input").value ;
var r = "" ;
for (i = 0; i < str.length; i++) {
r+=checkCase(str.charAt(i))+"" ;
}
document.getElementById("result").value = r ;
}
</script>

<textarea id="input" autofocus rows="10" cols="40" ></textarea>


<nobr>Bellow is the result of the alternative changed case of above sentence::




<textarea id="result" autofocus rows="10" cols="40" ></textarea>

 
Share this answer
 
v4
Comments
Member 11729765 30-May-15 3:10am    
He you got your Answer reply me to sanjay447 at gmail.com

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900