Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Please, explain part of the script

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #44671
    Kuzyo
    Participant

    Hi guys.

    Here is source http://codepen.io/Kuzyo/pen/bCdut I can’t undestand this part

     function highlight(node) {
    if (highlightedCell) {
    highlightedCell.style.backgroundColor = '';
    }
    highlightedCell = node;
    node.style.backgroundColor = 'red';
    }

    Please, explain somebody, what does mean **highlightedCell** ? Author didn’t get it by Id or in another way and what this **if (highlightedCell)** verify?

    Thanks in advance.

    #134683
    CrocoDillon
    Participant

    What Mottie said. Keep track which cell is highlighted so you can easily clear the highlight.

    #134688
    Kuzyo
    Participant

    sorry for noobie question but which background this code clear

    if (highlightedCell) {
    highlightedCell.style.backgroundColor = '';
    }

    everyone elements that has background??? :)))

    #134729
    CrocoDillon
    Participant

    No, that variable `highlightedCell` is a reference to the current element that has the highlight background.

    function highlight(node) {
    // checks if there is an element currently highlighted (with the background color)
    if (highlightedCell) {
    // if so, remove the background color from that element
    highlightedCell.style.backgroundColor = ”;
    }
    // highlightedCell is now a reference to the new highlighted element
    highlightedCell = node;
    // give new highlighted element the background color
    node.style.backgroundColor = ‘red’;
    }

    #134783
    Kuzyo
    Participant

    Thanks for detailed explanation. I understood everything.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.