Click here to Skip to main content
14,265,813 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi, I What is wrong with the following htmla and javascript code? I want convert uppercase to lowercase and lowercase ton uppercase onclick text where is (<p>).
<html>
<meta charset="utf-8">
<head>
    <title>text</title>
      <link rel="stylesheet" type="text/css" href="CSS.css">
<script language= "javascript">
        function convert() {
          var a=(<p>)
          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);

        }

        var element=document.getElementById("biografija");

        element.onclick = convert();

    </script>

</head>

<div id="contentwrapper">
<div id="contentcolumn">
<div class="innertube">
<h3 onclick="convert()" id="biografija" >Biografija</h3>
 <p>Coco Chanel, tikrasis vardas Gabrielle Bonheur Chanel; 1883 m. rugpjūčio 19 d. Somiure, Saumur – 1971 m. sausio 10 d. Paryžiuje– dizainerė, įkūrusi „Chanel" mados namus.
Ji buvo prancūzų modeliuotoja, išgarsėjusi trumpa čarlstono laikų suknele 1926 m. („maža juoda suknelė").
Chanel pirmoji atskleidė juodos spalvos elegantiškumą, rankines permetė per petį, jūreiviškus drabužius pavertė stilingu
 atostogų atributu. 1954 m. jai pavyko visus nustebinti „šanel" stiliaus kostiumėliais, kurie tapo jos mados namų firminiu ženklu.
Coco Chanel vardas įamžintas ir kvepaluose „Chanel Nr. 5". Nuo 1983 m. Šanel mados namams vadovauja Karlas Lagerfeldas</p>
<h3 id="suknelė">Suknelė</h3>
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I've not looked further, though the first line of the function is invalid.
The javascript debugger of your browser would have told you that, if you'd looked.

The following things are in your way.
1) var a=(<p>)</p> is not valid. Try getting the innerText property of the paragraph element instead.
2) The last line of your script is also no-good. You're assigning the result of the function validate (which is undefined, since it doesn't return anything) to the event handler for the 'click' event, of the h3 element. Uh-uh!
3) You're trying to assign the click handler of the h3 element before the page has finished loading.That is to say - when the code has been loaded and parsed, it executes immediately - this can be well before the HTML that it references has been loaded.
4) In addition to #3 - you've also added the function as an event handler in the html - this is unnecessary. Use one method or the other. I've used an implementation that waits for the DOM to be loaded before proceeding.

Here's a few changes made that will alleviate these problems and produce and output.

<html>
<meta charset="utf-8">
<head>
<title>text</title>

<script language= "javascript">
window.addEventListener('load', onDocLoaded, false);

function onDocLoaded(evt)
{
	var element=document.getElementById("biografija");
	element.addEventListener('click', convert, false);
}

function convert() 
{
//	var a=(<p>)
	var a = document.getElementById('tgtP').innerText;
	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>
 
</head>
 
<div id="contentwrapper">
<div id="contentcolumn">
<div class="innertube">
<h3 id="biografija" >Biografija</h3>
 <p id='tgtP'>Coco Chanel, tikrasis vardas Gabrielle Bonheur Chanel; 1883 m. rugpjūčio 19 d. Somiure, Saumur – 1971 m. sausio 10 d. Paryžiuje– dizainerė, įkūrusi „Chanel" mados namus.
Ji buvo prancūzų modeliuotoja, išgarsėjusi trumpa čarlstono laikų suknele 1926 m. („maža juoda suknelė").
Chanel pirmoji atskleidė juodos spalvos elegantiškumą, rankines permetė per petį, jūreiviškus drabužius pavertė stilingu
 atostogų atributu. 1954 m. jai pavyko visus nustebinti „šanel" stiliaus kostiumėliais, kurie tapo jos mados namų firminiu ženklu.
Coco Chanel vardas įamžintas ir kvepaluose „Chanel Nr. 5". Nuo 1983 m. Šanel mados namams vadovauja Karlas Lagerfeldas</p>
<h3 id="suknelė">Suknelė</h3>
   
v2
Comments
   
5ed.
—SA

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100