1

I have created a 16x16 grid. I'm trying to put a button above the grid, which when the user clicks, will prompt them for new grid dimensions (between 1 and 64). For example, if you click the button and input 25, the dimensions of the grid will change from 16x16 to 25x25.

I'm having trouble in figuring out how to get the user's input into a javascript function, so it outputs the updated grid. Code here:
HTML:

<!doctype html>
<html>

<head>
<title>SketchPad</title>
<link rel="stylesheet" type="text/css" href="style.css" >
</head>

<body>
<h1> SketchPad </h1>

<div id="container">

</div>
<button class="button" type="button" onclick="myFunction()">Choose Your Grid</button>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>        
<script src="jquery.js"></script>
</body>

</html>

CSS:

h1 {
text-align: center;
color: black;
}
tr, td, table {
border-style: solid;
border-width: 1px;
background-color: gray;
margin: auto;
height: 25px;
width: 525px;
margin-top: 20px;
}
td {
transition: .3s background-color;
}
td:hover {
background-color: red;
}
button {
height: 25px;
width: 225px;
margin: 0 auto;
position: relative;
top: 50%;
left: 40%;
margin-top: -40px;
}

Javascript:

var rows=16;
var cols=16;       

document.write("<table>");
for (i=0; i<rows; i++) {
document.write("<tr>");
  for (j=0; j<cols; j++) {
    document.write("<td>"+"</td>");
  }
document.write("</tr>");
}
document.write("</table>");

$( "td").css("color", "red");

$(".button").click(function() {
prompt("Please select your grid size");
});

function grid(rows, cols) {
//function goes here//
}
4
  • If the javascript code is contained in an outer file, it is never included in the html Commented Jul 7, 2015 at 19:01
  • Are you referring to this line? <button class="button" type="button" onclick="myFunction()">Choose Your Grid</button> Commented Jul 7, 2015 at 19:07
  • no, i'm referring to the fact that in your <script src=""></script> there is no reference to the file with the javascript. unless you named it jquery.js. did you? Commented Jul 7, 2015 at 19:14
  • yes, i did name it jquery.js. and it appears to work fine. i'm confused...also new to this, so maybe i'm missing something. Commented Jul 7, 2015 at 19:17

1 Answer 1

2

Try something like the following.

  1. Write a function which empties your container and redraws your grid inside it.
  2. Initialize your grid in a document.ready() hander.
  3. Define an event handler for your button click, which prompts to redraw the grid at a user-defined size.

$(document).ready(function() {
  grid(16,16);
});

$(".button").click(function() {
  var size = prompt("Please select your grid size");
  grid(size, size);
});

function grid(rows, cols) {
  var table = "<table>";
  var size = (1 / rows * 525) + "px";
  
  for (i=0; i<rows; i++) {
    table += "<tr>";
    for (j=0; j<cols; j++) {
      table += "<td>"+"</td>";
    }
    table += "</tr>";
  }
  table += "</table>";
  
  $("#container").empty().append(table);
  $("tr").css("height", size);
  $("td").css("color", "red").css("width", size);
}
h1 {
  text-align: center;
  color: black;
}
table {
  height: 525px;
  width: 525px;
}
tr, td, table {
  border-style: solid;
  border-width: 1px;
  background-color: gray;
  margin: auto;
  margin-top: 20px;
}
td {
  transition: .3s background-color;
}
td:hover {
  background-color: red;
}
button {
  height: 25px;
  width: 225px;
  margin: 0 auto;
  position: relative;
  top: 50%;
  left: 40%;
  margin-top: -40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1> SketchPad </h1>

<div id="container">
</div>

<button class="button" type="button">Choose Your Grid</button>

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

3 Comments

Thanks for this Dave. I will work with this and try to make some adjustments. The two things I would change: I want the boxes to stay square and not change to rectangles based on dimensions. And second, I want the grid to stay within the same sized box container, so there are just MORE boxes within it, instead of expanding the entire size of the grid.
OK I fixed the code so the inside cells stay square and all remain the same size. Is that what you want?
this looks great in terms of the square boxes. now it's just that the size of the entire grid gets bigger or smaller depending on the dimensions. instead of keeping the exact same size outer grid, and just changing the grids inside it.

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.