Reputation: 48
ok, I"m having serious problems with the DOM when I do ANY kind of Javascript. the following snippet of code doesn't work, for some reason that I can't begin to fathom. Whenever I try to use the getElementById() function, the script stops working. please tell me what I'm doing wrong.
var total=0;
document.write("test");
function quickTotal(price,id){
alert(price)
alert(id)
var object=getElementById(id)
if(object.checked == 1){
total=parseFloat(total)+parseFloat(price)
alert("add")
}
if(object.checked == 0){
total=parseFloat(total)-parseFloat(price)
alert("subtract")
}
alert(total)
//document.floater.price.innerHTML("test")
}
Upvotes: 0
Views: 231
Reputation: 87228
Try using document.getElementById(id)
(prefix it with document
, since the method is on the document
object, not window
).
Update: an example with your code:
<input type="checkbox" id="myChkBox" />
<input type="button" onclick="quickTotal(30, 'myChkBox');" value="Click me" />
<script type="text/javascript">
var total = 0;
document.write("test");
function quickTotal(price, id) {
alert(price);
alert(id);
var object = document.getElementById(id);
if (object.checked == 1) {
total = parseFloat(total) + parseFloat(price);
alert("add");
}
if (object.checked == 0) {
total = parseFloat(total) - parseFloat(price);
alert("subtract");
}
alert(total);
//document.floater.price.innerHTML("test")
}
</script>
Upvotes: 6