Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section>
  <h1>Tic-Tac-Toe</h1>
  <p>Current Player: <span class="player">O</span></p>
  <div class="grid">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
  </div>
  <button class="reset">Reset</button>
</section>
              
            
!

CSS

              
                :root {
--cell-size: 2.5em;
}

body {
  display: flex;
  justify-content: center;
  text-align: center;
}

.grid {
  display: grid;
  grid-template:
  "c c c"
  "c c c"
  "c c c";
  margin-bottom: .5em;
}

.cell {
  border-right: 1px solid #333;
  border-bottom: 1px solid #333;
  font-size: 2.5em;
  height: var(--cell-size);
  line-height: var(--cell-size);
  text-align: center;
  width: var(--cell-size);
}

.cell:nth-child(3n) {
  border-right: none;
}

.cell:nth-child(n + 7) {
  border-bottom: none;
}

.cell--x::after {
  content: 'X';
}

.cell--o::after {
  content: 'O';
}
              
            
!

JS

              
                // To do:
// 1) Clicking a cell adds X or O
// 2) Update "Current Player"
// 3) Reset button
// Relevant class names:
// "cell"
// "cell--x"
// "cell--o"
// "reset"

// To do:
// 1) Clicking a cell adds X or O
// 2) Update "Current Player"
// 3) Reset button
// Relevant class names:
// "cell"
// "cell--x"
// "cell--o"
// "reset"
// "player"

// Variables
var playerO = 'O';
var playerX = 'X';
var currentPlayer = playerO;
var resetButton = document.querySelector('.reset');
var playerStatusElement = document.querySelector('.player');
var cells = document.querySelectorAll('.cell'); //[cell, cell, cell, cell ,cell,  cell, ....]
console.log(cells); // take a look at what the querySelectorAll array looks like in the console

// Functions
function myFunction() { // this is how you define a function
  // code inside myFunction goes here
}

function drawing(event) { // HINT element.classList.add('myClassName');
  console.log(event); // take a look at this in the "Console" tab of your inspector to get all the intricate details of the click event!
  // All we'll need is event.target to refer to what cell was just clicked
  if ( currentPlayer === playerO ) {
    event.target.classList.add('cell--o');
    currentPlayer = playerX;
  } else {
    event.target.classList.add('cell--x');
    currentPlayer = playerO;
  }
  // update the playerStatusElement hint  playerStatus.innerHTML = 'my new content';
}

// Event Listeners
for (var i = 0; i < cells.length; i++) {
  cells[i].addEventListener('click', drawing);
}

// Hint: for the reset button, loop over every cell and cells[i].classList.remove('cell--o', 'cell--x').
// Hint: use a for loop!
// Hint: copy, past, and modify the for loop used to attach the drawing function to the cell click event

              
            
!
999px

Console