views:

772

answers:

4

Now I'm sure this is super easy, I have a function called "remove_deposit" which I want it to make the checkbox false if it's true. But I can't seem to get it to work..

function remove_deposit() {
    if(document.getElementById('deposit').checked == true) {
     document.getElementById('deposit').checked == false;
    };
};

Am I at least on the right track?? lol

+2  A: 
function remove_deposit() {
    if(document.getElementById('deposit').checked == true) {
        document.getElementById('deposit').checked = false;
    };
};

You were doing a comparison instead of simply setting the checked attribute to false.

Paolo Bergantino
+3  A: 
Coding With Style
I suppose not, but I don't exactly see how I'm being rude either.
Coding With Style
I removed the unnecessary beginning remark.
Josh Stodola
I didn't mean to put down the author for asking the question; I was just put off by the pointless if statement. It looked like a waste to me. Anyhow, I'd remove the comment but Josh beat me to the punch. :-/All's well that ends well?
Coding With Style
Do you have any documentation on your last statement? Everything I have read points to getElementById being the fastest, bar none, way of accessing an element in a document. Browsers keep a hash table of the IDs in the document - which is possible since they are supposed to be unique - allowing for very fast lookups. Do you have anything that contradicts this? I can't find any documentation to support my claim, although I'm sure I've read it, so I'm wondering if you have anything on your end.
Paolo Bergantino
All's well, although am also interested in Paolo's question for personal reference.
Ian Elliott
I don't. I just arrived at that conclusion because I figured going through forms means you need to look through less. I may be mistaken, however, so I suppose some testing is in order. I'll edit this later with my results.
Coding With Style
Results added. Looks like Paolo is right.
Coding With Style
+1 for doing the legwork on this. Nice job.
Paolo Bergantino
+1 for settling it like a man (with evidence). It'd be especially cool if you could prove that just setting false is more efficient that doing the comparison.
Josh Stodola
Will do. :-) (Stackoverflow doesn't like short comments so I'm adding this pointless sentence.)
Coding With Style
I think this shows where browser vendors should be next looking for DOM improvements.
staticsan
Done. It seems I'm right, though not by a lot.
Coding With Style
It all really depends on which is the more common state. If it's usually false then it's better to perform the check. Anyways, very well done.
Ian Elliott
Thanks. And, you're right.
Coding With Style
Awesome! Excellent follow-up. Goes well with the "With Style" part of your name :-D
Josh Stodola
Thank you. :-) (Here's another pointless sentence.)
Coding With Style
A: 

Ok so now I feel stupid.. lol thank you everyone for helping me out, am still new to javascript :) Damn that double equal sign :S

SoulieBaby
Haha. No problem. We all make mistakes the first time. :-)Usually the problem is the other way around with people using the assignment operator (=) in the if statement.
Coding With Style
+1  A: 

You have an small error in your code sample on line 3. You've used a double equals ==

It should read:

document.getElementById('deposit').checked = false;

firecall