Bro, I Agree… but there are a lot of pitfalls, if you don’t have such discipline in writing your code, and some experience in document parsing.
This time, I wont… hehe… No…not all the time, and not restricted to…. What happen here is the time he writes this way:
<input type=checkbox Id="Add" onClick="Add()">
And call javascript
:
if (document.getElementById("Add").checked==True)
wHAaaaaat? did JavaScript parse it both as
'Add' Well… there are a lot of ways to solve having the same
function name and
id but however, the easiest and fastest way is to apply discipline, you as a programmer… by adding two pre/postfix char or whatever naming rules you’d like to apply.
Eg. IdSum and fnSum (I got two characters here as my ident.)
It’s because you are adding two strings, not two integers… yeah..google the isnan its very important.
You’re right… but I won’t suggest it that way… instead I will pass an object to my function by using
'this' and directly manipulate it, by calling its method. there is a very big difference by calling an object METHOD, and running different FUNCTION (which requires different kinds of process)
Here’s my sample code… Glad if someone explained hehehe coz its coffee time here…
PHP Code:
<html>
<head>
<script type="text/javascript">
function fnAdd(val){
if (val.checked) {
sum =parseFloat(calculator.input1.value) + parseFloat(calculator.input2.value);
if (!isNaN(sum)) document.getElementById("output").innerHTML= sum
else document.getElementById("output").innerHTML= "Invalid data."
}
else {
document.getElementById("output").innerHTML= ""
}
}
</script>
</head>
<body>
<form name=calculator>
<input type=checkbox Id="idAdd" onClick="fnAdd(this)">
<input type=text name=input1>
<input type=text name=input2>
<div id=output> </div>
</form>
</body>
</html>
val.checked vs. document.getElementById("Add").checked==True)
Which one is better? I'm not web guy anyway...
Hope this may help... ![Smiley](images/smilies/smiley.gif)