views:

68

answers:

3

Im working on a script which removes the default values in form elements using Prototype and LightView. The scripts works fine in Safari, but not at all in FireFox (3.5.5).

This fires when a lightview is triggered.

document.observe('lightview:opened', function() {
            if($('contact_form')) {
                var defaults = new Array();
                var ins = $('contact_form').getElements();
                var inlen = ins.length;
                for(i=0; i < inlen; i++) {
                    alert(i)
                    if($(ins[i]).readAttribute('type') != 'image') {
                        defaults[ins[i].name] = $(ins[i]).value;
                        $(ins[i]).observe('focus', checkDefault.bind(event, ins[i]));

                    }

                }

                function checkDefault(name, event) {
                    alert(name.name)
                    if($(name).value == defaults[name.name]) {
                        alert(defaults[name.name])
                        $(name).value = '';
                    }
                }
        }
        });

The strange this is, when I check for the length of inlen the proper number is alerted, but when I alert 'i', only the first number is alerted. I can't figure out why this is happening.

Any ideas what's wrong here?

Here is the address of the problem: http://bearing.krd-design.net/

Thanks Rich

+1  A: 

It sounds like a timing issue; the alerts slow execution to the point where your code works in FF. Do you use FireBug?

Upper Stage
Yes, FireBug is running and it returns no problems.Maybe Ill reconstruct the script to see whats up.
Richard Testani
Add breakpoints in your script. If you don't find the answer quickly, this problem might be a timing issue; events are fired earlier or later in different browsers.
Upper Stage
I found after I add the observe statement, the script breaks in FF.$(ins[i]).observe('focus', checkDefault.bind(event, ins[i]));Im not sure how to troubleshoot this.
Richard Testani
What is the error?
Upper Stage
There isn't any errors, if I alert(i), it only alerts 0 then stops.Strange.Wonder if its LightView related.
Richard Testani
I guess I don't understand your comment above: "the script breaks." I assumed that meant an error...
Upper Stage
Another idea: don't use alerts. Try console.log() to send info to the console. This might help you isolate a timing issue.
Upper Stage
ill try this - when I say breaks - i mean it seems like it's leaving the loop or the script is halting but without error.
Richard Testani
+2  A: 

I'm not sure if this would cause that problem, but you are missing var:

for(var i=0; i < inlen; i++) {

Also, there are no semi-colon's after any of your alert()s.

Try correcting those, and see if it makes a difference.

EDIT:

As pointed out by Matt, in the comments: neglecting var creates the variable in the global scope. This could cause a problem if prototypejs also uses a global variable i (but I sincerely doubt that).

EDIT 2:

Another possibility is the array accessing by input name:

defaults[ins[i].name] = $(ins[i]).value;

Try declaring defaults as an object:

var defaults = {};
Joel Potter
If I recall correctly, declaring a variable without var automatically gives it a global scope. This can result in strange problems, but I don't believe it is the source of his error - I'd believe the missing ; the culprit.
Matt
Ive removed the alerts altogether, and the script still isn't woking in FF.
Richard Testani
`;` in js is optional if it is followed by a line wrap
frunsi
Tested this out, with same results.
Richard Testani
@frunsi didn't know that! Thanks!
Matt
A: 

First of all, I think it is only going to i=0 is probably because the JS broke during the first iteration. To troubleshoot, I'd put alert() in between lines and see which line is breaking it.

My guess of the problem should be in the line:

$(ins[i]).observe('focus', checkDefault.bind(event, ins[i]));

The first parameter of the 'bind' function should be context and you are passing in 'event'. 'event' isn't defined and it should be the context or scope.

In your case, I think you can just use:

$(ins[i]).observe('focus', checkDefault(ins[i]));
JONYC