0

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

1
  • 1
    You only need one "=" on the third line. Is that a typo or did you copy paste? Commented Jul 3, 2009 at 0:24

3 Answers 3

3
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.

Sign up to request clarification or add additional context in comments.

Comments

3
function remove_deposit() {
    document.getElementById('deposit').checked = false
}

That's all you need. The reason why your code wasn't working was because you used two equals signs, which is a comparison operator, instead of one equals sign, which is the assignment operator.

Addendum: I removed the if statement since it doesn't really do anything useful afaict. If you did this to optimize the code, then I'd just like to point out that checking the if statement will probably be slower than just setting the checkbox to false. Also, you don't need to end every line with a semi-colon in JavaScript. You can do that if you want to put multiple commands on a single line, but otherwise it's not necessary.

Results are in.

If most of the time people running the javascript to set the checkbox to false do have the checkbox set to true, skipping the if statement is faster. Otherwise, if most of the time the checkbox is set to false, then the if statement would be faster. With a 1:1 ratio, no if statement is preferred.

The checkbox would have to be set to false at least 54% of the time for the if code to be more efficient.

http://img33.imageshack.us/img33/4260/results.png http://img33.imageshack.us/img33/4260/results.png

Side-note: If it's a checkbox, it's probably in a form, so you could also access it with the old-fashioned method document.formName.elementName instead of document.getElementById(). Because the form method doesn't need to traverse the DOM like getElementById does, I think that would be faster.

The results are in.

With 0 IDs preceding it, document.test_form.checkbox (DTFC) is slower than document.getElementById('checkbox') (GEBI). With 100 IDs preceding it, document.test_form.checkbox is still slower than document.getElementById('checkbox').

http://img8.imageshack.us/img8/6683/resultsw.png http://img8.imageshack.us/img8/6683/resultsw.png

I guess that settles it.

PS: These were all tested on Firefox using Firebug. I cannot make any claims about the efficiency of Safari's WebKit, Konqueror's KJS, IE's proprietary engine, or Google Chrome's V8. I'm guessing they should wind up somewhat similar to these, but they could be different.

10 Comments

I suppose not, but I don't exactly see how I'm being rude either.
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?
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.
All's well, although am also interested in Paolo's question for personal reference.
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.
|
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.