Click here to Skip to main content
16,001,934 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi, I What is wrong with the following htmla and javascript code?
XML
<!DOCTYPE html>
<html>
<body>


<p id="demo">Click me.</p>

<script>
document.getElementById("demo").addEventListener("click", m);

function m() {
   var a=document.getElementById("demo").innerHTML;
   var b="";
   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>

</body>
</html>
Posted
Comments
Kornfeld Eliyahu Peter 30-Dec-14 11:27am    
How can we know - you tell!

Opening bracket for else is missing:
SQL
else
{
 
Share this answer
 
Nothing is wrong with this function, but… What are you doing? At the moment of the call to addEventListener, the object m (function) is not yet initialized. Move this call to put it after m = …

—SA
 
Share this answer
 
i want convert "click me" to lower case or upper case
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 30-Dec-14 11:39am    
Do not post your comments as solution, please!
The problem is that you code has errors...two in fact...
1. m is unknown at the point you try to use it. Remember that JavaScript is an interpreted (script!) language and not compiled so as long the interpreter doesn't got to the line with m, it knows nothing of it...The solution, obviously is to change the order...
2. Missing { at the beginning of the else part.

All this problems can be easily spotted using the debugger of your browser - use them and you will learn a lot of about how your code behaves and about the ways to fix it...
 
Share this answer
 

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