0

I am programming a sudoku puzzle (http://www.jsfiddle.net/sZ7Aq/4/). It works okay on IE, but when I try it on Google Chrome, the button doesn't do anything when I click on it. Is there a way to fix it so it works on all browsers?

Please note: I haven't finished it so there isn't a puzzle generating function. You must enter all numbers yourself.

Here is my main() function (if you did not click on the link yet):

function main() {
    getcellVal();
    if (validate() == false) {
        alert("Something's not right!");
        return false;
    }
    alert("Good job!");
    return true;
}

My button:

<button onclick="javascript: main()">Check my answer</button>
Oliver Ni
  • 2,606
  • 7
  • 29
  • 44

3 Answers3

0

Use .addEventListener('click', main); For instance:

var check = document.getElementById('check');
check.addEventListener('click', main);

and of course add id="main" and remove the onclick from your button.

Updated fiddle

Then open your console and fix the other bugs...

philwills
  • 985
  • 4
  • 8
0

From your jsfiddle, the error is in your getcellValue() function. This code is incorrect:

colVal  =  [[x0y0.value, x0y1.value, x0y2.value, x0y3.value, x0y4.value, x0y5.value, x0y6.value, x0y7.value, x0y8.value],

I would suggest you change your input elements to have an id attribute that matches the name. Like this:

<input type="text" name="x0y0" id="x0y0">

Then change your getcellVal() function to use the getElementById function.

colVal  =  [[document.getElementById("x0y0").value, ...],

It will be much more verbose, but it will work in more browsers.

Sam
  • 2,166
  • 2
  • 20
  • 28
0

Few things need to be corrected:

  • Instead of defining an event such as javascript: main() inline, use unobtrusive solution to register the necessary event handlers programmatically.

Eg:

<button id="buttonid">Check my answer</button>

window.onload = function() {
document.getElementById('buttonid').onclick = main;
};
  • You cannot access the DOM elements just by specifying their names. In your case x0y0.value will not return anything. Instead use id or class name to access the set of elements.

Eg:

<input type="text" name="x0y0" id="x0y0">

In javascript,

document.getElementById("x0y0").value
Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22
  • using .onclick is not much better... using .addEventListener('click', function) is much more accepted, as you can add more than one... – philwills Nov 21 '13 at 04:04