Home > Enterprise >  Javascript: how to function if class exists or change
Javascript: how to function if class exists or change

Time:04-24

Thank you, everyone! I am keep going design my game with Javascript.

There is box, id called 'box1'. Also, there is a hidden button. I have a function to change the box1 class.

I want to change the visibility if the box1 has the class "glow". In fact, I am planning to check 9 boxes class. Therefore, I may use && for "if" condition.

$(document).ready(function() {
  if (document.$('#box1').classList.contains('.glow')) {
    $(".btn-warning").css('visibility', 'visible');
}});

CodePudding user response:

something like you can design your code

You have a 9 blocks with its like box1, box2, so on

Loops through get all class nodes

$("div[id^='box']").foreach(function(){
  if($(this).hasClass(".glow"))
  {
  // your logic
  $(".btn-warning").css('visibility', 'visible');
  }
}

I hope this will work for you

CodePudding user response:

Thank you everyone for your comments. I change my knowledge.

I finish the code myself.

'gameboxsize' is the div for 9boxes.

setInterval(function(){
  var gamebox = document.getElementById('gameboxsize');
  var nodesSameClass = gamebox.getElementsByClassName('glow');
  console.log(nodesSameClass.length);
  var winNum = nodesSameClass.length;
  if (winNum === 9) {
  $(".btn-warning").css('visibility', 'visible');
}
});
  • Related