PDA

View Full Version : Javascript variable: where have I gone wrong?



nikonian
February 15th, 2012, 03:22 AM
Hi there. Here is some code which is (supposed to) change the paragraph, when the button is clicked. I want to compress my code into variables, but nothing works :(



<html>
<head>


<script type="text/javascript" >
var do = function () {document.getElementById("para").innerHTML=("SOME");};

function clicked(){
do();
}
</script>





</head>
<body>
<p id="para">This due to change</p>
<button type="button" id="butt1" onclick="clicked()">CLICK ME</button>
</body>
</html>


If you could help me? Thanks :)

myrtle1908
February 15th, 2012, 03:48 AM
A few observations ...

1. Why don't you just call "do" directly. In your trivial example there is no need for a "clicked" handler.

2. innerHTML=("SOME"); ... this is invalid syntax. Should be:-


innerHTML="SOME";

3. Debug/Step-through your code with Firebug or Chrome Dev Tools.

nikonian
February 15th, 2012, 04:45 AM
A few observations ...

1. Why don't you just call "do" directly. In your trivial example there is no need for a "clicked" handler.

2. innerHTML=("SOME"); ... this is invalid syntax. Should be:-


innerHTML="SOME";3. Debug/Step-through your code with Firebug or Chrome Dev Tools.


Would you mind showing me the code that you would use, for my example? Having only shown me half the answer, as a newcomer, I am now *twice* as lost. You could as easily have shown me the suggested code, instead of explaining it... which is subject to interpretation, and in my case, erroneously so.

Thank you.

nikonian
February 15th, 2012, 02:28 PM
Anyone? Hello? :)

k@e
February 15th, 2012, 02:38 PM
"do" is a reserved keyword in JavaScript, you cannot name variables or functions the same as keywords. Change it to something else like "dosomething".

nikonian
February 15th, 2012, 04:34 PM
"do" is a reserved keyword in JavaScript, you cannot name variables or functions the same as keywords. Change it to something else like "dosomething".

Thank you so much. You have sorted out my issue.