views:

25

answers:

1

My HTML contains a fieldset, six text input fields formatted in a table, and a submit button. The submit button includes the attribute onClick="check()" and it's worked with simpler alerts so I know that's not the problem. Here's my JavaScript:

var $ = function (id) { return document.getElementById(id); }

function check() {  
    var x = $("myForm");  
    var y;  
    for (var i=1; i < x.length; i++) {  
        y += (x.elements[i].value + "<br />;");}  
    alert("values: <br />" + y)  
    }

When I click the submit button, I don't get any alert at all. What should I do?

A: 

This is probably not the answer you're looking for.

If you're on Firefox, install the Firebug addon, open it up and see what errors you are getting.

If you're on Chrome, open up the Javascript console (Ctrl+Shift+J), and see what error your are getting.

Otherwise: choose your weapon :)

Znarkus
I did that thing on Chrome. When I clicked submit, I got this: "Uncaught TypeError: Cannot read property 'length' of null"
So I thought to temporarily change the part with the length in the loop to 7. Then it told me "Uncaught TypeError: Cannot read property 'elements' of null". Why is x null?
If x is `null` I can only assume that there is no `myForm` element, when the code executes.
Znarkus
Right you are! D'oh. I had named the element <code>myForm</code> but I failed to give it an ID.