How can I target a box in order to hide it with jQuery?











up vote
2
down vote

favorite












I'm trying to make a function in order to hide the boxes with their corresponded number, but I have no idea on how to do it. I tried to figure out how to do it, but every method I come up with ends up on an error.



I'm using jQuery library and Bootstrap.



CSS :



.random {
width: 100 px;
height: 100 px;
background - color: lightgreen;
margin: 30 px;
}


HTML :



<div class="container">
<div class="row">
<div class="col">
<div class="container text-center">
<div id="box_1" class="random">Box 1</div>
<div id="box_2" class="random">Box 2</div>
<div id="box_3" class="random">Box 3</div>
<div id="box_4" class="random">Box 4</div>
<div id="box_5" class="random">Box 5</div>
<div id="box_6" class="random">Box 6</div>
<div id="box_7" class="random">Box 7</div>
<div id="box_8" class="random">Box 8</div>
<div id="box_9" class="random">Box 9</div>
<div id="box_10" class="random">Box 10</div>
</div>
</div>
<div class="col">
<button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
</div>
</div>
</div>


JavaScript :



$(document).ready(function() {
$(".task_hidden").hide();
var box_1 = $('#box_1');
var box_2 = $('#box_2');
var box_3 = $('#box_3');
var box_4 = $('#box_4');
var box_5 = $('#box_5');
var box_6 = $('#box_6');
var box_7 = $('#box_7');
var box_8 = $('#box_8');
var box_9 = $('#box_9');
var box_10 = $('#box_10');
});

function NumberFunction(random) {
console.log(random);
if (random = 1) { box_1.hide(); }
if (random = 2) { box_2.hide(); }
if (random = 3) { box_3.hide(); }
if (random = 4) { box_4.hide(); }
if (random = 5) { box_5.hide(); }
}









share|improve this question




























    up vote
    2
    down vote

    favorite












    I'm trying to make a function in order to hide the boxes with their corresponded number, but I have no idea on how to do it. I tried to figure out how to do it, but every method I come up with ends up on an error.



    I'm using jQuery library and Bootstrap.



    CSS :



    .random {
    width: 100 px;
    height: 100 px;
    background - color: lightgreen;
    margin: 30 px;
    }


    HTML :



    <div class="container">
    <div class="row">
    <div class="col">
    <div class="container text-center">
    <div id="box_1" class="random">Box 1</div>
    <div id="box_2" class="random">Box 2</div>
    <div id="box_3" class="random">Box 3</div>
    <div id="box_4" class="random">Box 4</div>
    <div id="box_5" class="random">Box 5</div>
    <div id="box_6" class="random">Box 6</div>
    <div id="box_7" class="random">Box 7</div>
    <div id="box_8" class="random">Box 8</div>
    <div id="box_9" class="random">Box 9</div>
    <div id="box_10" class="random">Box 10</div>
    </div>
    </div>
    <div class="col">
    <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
    </div>
    </div>
    </div>


    JavaScript :



    $(document).ready(function() {
    $(".task_hidden").hide();
    var box_1 = $('#box_1');
    var box_2 = $('#box_2');
    var box_3 = $('#box_3');
    var box_4 = $('#box_4');
    var box_5 = $('#box_5');
    var box_6 = $('#box_6');
    var box_7 = $('#box_7');
    var box_8 = $('#box_8');
    var box_9 = $('#box_9');
    var box_10 = $('#box_10');
    });

    function NumberFunction(random) {
    console.log(random);
    if (random = 1) { box_1.hide(); }
    if (random = 2) { box_2.hide(); }
    if (random = 3) { box_3.hide(); }
    if (random = 4) { box_4.hide(); }
    if (random = 5) { box_5.hide(); }
    }









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to make a function in order to hide the boxes with their corresponded number, but I have no idea on how to do it. I tried to figure out how to do it, but every method I come up with ends up on an error.



      I'm using jQuery library and Bootstrap.



      CSS :



      .random {
      width: 100 px;
      height: 100 px;
      background - color: lightgreen;
      margin: 30 px;
      }


      HTML :



      <div class="container">
      <div class="row">
      <div class="col">
      <div class="container text-center">
      <div id="box_1" class="random">Box 1</div>
      <div id="box_2" class="random">Box 2</div>
      <div id="box_3" class="random">Box 3</div>
      <div id="box_4" class="random">Box 4</div>
      <div id="box_5" class="random">Box 5</div>
      <div id="box_6" class="random">Box 6</div>
      <div id="box_7" class="random">Box 7</div>
      <div id="box_8" class="random">Box 8</div>
      <div id="box_9" class="random">Box 9</div>
      <div id="box_10" class="random">Box 10</div>
      </div>
      </div>
      <div class="col">
      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
      </div>
      </div>
      </div>


      JavaScript :



      $(document).ready(function() {
      $(".task_hidden").hide();
      var box_1 = $('#box_1');
      var box_2 = $('#box_2');
      var box_3 = $('#box_3');
      var box_4 = $('#box_4');
      var box_5 = $('#box_5');
      var box_6 = $('#box_6');
      var box_7 = $('#box_7');
      var box_8 = $('#box_8');
      var box_9 = $('#box_9');
      var box_10 = $('#box_10');
      });

      function NumberFunction(random) {
      console.log(random);
      if (random = 1) { box_1.hide(); }
      if (random = 2) { box_2.hide(); }
      if (random = 3) { box_3.hide(); }
      if (random = 4) { box_4.hide(); }
      if (random = 5) { box_5.hide(); }
      }









      share|improve this question















      I'm trying to make a function in order to hide the boxes with their corresponded number, but I have no idea on how to do it. I tried to figure out how to do it, but every method I come up with ends up on an error.



      I'm using jQuery library and Bootstrap.



      CSS :



      .random {
      width: 100 px;
      height: 100 px;
      background - color: lightgreen;
      margin: 30 px;
      }


      HTML :



      <div class="container">
      <div class="row">
      <div class="col">
      <div class="container text-center">
      <div id="box_1" class="random">Box 1</div>
      <div id="box_2" class="random">Box 2</div>
      <div id="box_3" class="random">Box 3</div>
      <div id="box_4" class="random">Box 4</div>
      <div id="box_5" class="random">Box 5</div>
      <div id="box_6" class="random">Box 6</div>
      <div id="box_7" class="random">Box 7</div>
      <div id="box_8" class="random">Box 8</div>
      <div id="box_9" class="random">Box 9</div>
      <div id="box_10" class="random">Box 10</div>
      </div>
      </div>
      <div class="col">
      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
      </div>
      </div>
      </div>


      JavaScript :



      $(document).ready(function() {
      $(".task_hidden").hide();
      var box_1 = $('#box_1');
      var box_2 = $('#box_2');
      var box_3 = $('#box_3');
      var box_4 = $('#box_4');
      var box_5 = $('#box_5');
      var box_6 = $('#box_6');
      var box_7 = $('#box_7');
      var box_8 = $('#box_8');
      var box_9 = $('#box_9');
      var box_10 = $('#box_10');
      });

      function NumberFunction(random) {
      console.log(random);
      if (random = 1) { box_1.hide(); }
      if (random = 2) { box_2.hide(); }
      if (random = 3) { box_3.hide(); }
      if (random = 4) { box_4.hide(); }
      if (random = 5) { box_5.hide(); }
      }






      javascript jquery html css






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 21:03









      halfer

      14.2k757105




      14.2k757105










      asked Jun 15 at 3:47









      Alejandro Carvajal Sarria

      294




      294
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Unlike other answers, with each click, a box will be deleted (this is sure to happen) but in other answers may be a few clicks to remove a box.



          $(document).ready(function(){

          var listOfRemovedNumbers =;
          var lengthBoxs = $('[id^=box_]').length;

          function getRandomNumber(){
          do {
          var randomNumber = Math.floor(Math.random() * 10) + 1;
          } while(listOfRemovedNumbers.includes(randomNumber) );
          return randomNumber;
          }

          $('.btn').click(function(){
          var randomNumber = getRandomNumber();
          $('#box_'+randomNumber).remove();
          listOfRemovedNumbers.push(randomNumber);
          if ((listOfRemovedNumbers.length == lengthBoxs ))
          $('.btn').attr("disabled", "disabled").html('All Removed');
          })

          })





          $(document).ready(function(){

          var listOfRemovedNumbers =;
          var lengthBoxs = $('[id^=box_]').length;

          function getRandomNumber(){
          do {
          var randomNumber = Math.floor(Math.random() * 10) + 1;
          } while(listOfRemovedNumbers.includes(randomNumber) );
          return randomNumber;
          }

          $('.btn').click(function(){
          var randomNumber = getRandomNumber();
          $('#box_'+randomNumber).remove();
          listOfRemovedNumbers.push(randomNumber);
          if ((listOfRemovedNumbers.length == lengthBoxs ))
          $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
          })
          })

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
          <div class="container">
          <div class="row">
          <div class="col">
          <div class="container text-center">
          <div id="box_1" class="random">Box 1</div>
          <div id="box_2" class="random">Box 2</div>
          <div id="box_3" class="random">Box 3</div>
          <div id="box_4" class="random">Box 4</div>
          <div id="box_5" class="random">Box 5</div>
          <div id="box_6" class="random">Box 6</div>
          <div id="box_7" class="random">Box 7</div>
          <div id="box_8" class="random">Box 8</div>
          <div id="box_9" class="random">Box 9</div>
          <div id="box_10" class="random">Box 10</div>
          </div>
          </div>
          <div class="col">
          <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
          </div>
          </div>
          </div>








          share|improve this answer






























            up vote
            3
            down vote













            Your code was not working fine because you had declared variables box_1, box_2 inside the ready function which were not able outside it i.e. not available in NumberFunction function. Also, you were assigning variables in if rather than comparing. = is assignment and == or === is for comparison.



            You can improve your code to following:






            $(document).ready(function() {
            $(".task_hidden").hide();
            });

            function NumberFunction(random) {
            $("#box_" + random).hide();
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <div class="container">
            <div class="row">
            <div class="col">

            <div class="container text-center">

            <div id="box_1" class="random">Box 1</div>
            <div id="box_2" class="random">Box 2</div>
            <div id="box_3" class="random">Box 3</div>
            <div id="box_4" class="random">Box 4</div>
            <div id="box_5" class="random">Box 5</div>
            <div id="box_6" class="random">Box 6</div>
            <div id="box_7" class="random">Box 7</div>
            <div id="box_8" class="random">Box 8</div>
            <div id="box_9" class="random">Box 9</div>
            <div id="box_10" class="random">Box 10</div>

            </div>

            </div>
            <div class="col">

            <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
            </div>
            </div>
            </div>








            share|improve this answer























            • It was also not working because he was using = instead of == or ===.
              – shiftweave
              Jun 15 at 3:53










            • @shiftweave - True :)
              – Nikhil Aggarwal
              Jun 15 at 3:54






            • 1




              Thank you guys!
              – Alejandro Carvajal Sarria
              Jun 15 at 5:05










            • @AlejandroCarvajalSarria - Welcome mate :)
              – Nikhil Aggarwal
              Jun 15 at 5:05


















            up vote
            1
            down vote
















            .random {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            margin: 30px;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

            <div class="container">
            <div class="row">
            <div class="col">

            <div class="container text-center">

            <div id="box_1" class="random">Box 1</div>
            <div id="box_2" class="random">Box 2</div>
            <div id="box_3" class="random">Box 3</div>
            <div id="box_4" class="random">Box 4</div>
            <div id="box_5" class="random">Box 5</div>
            <div id="box_6" class="random">Box 6</div>
            <div id="box_7" class="random">Box 7</div>
            <div id="box_8" class="random">Box 8</div>
            <div id="box_9" class="random">Box 9</div>
            <div id="box_10" class="random">Box 10</div>

            </div>

            </div>
            <div class="col">

            <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
            </div>
            </div>
            </div>
            <script>
            function NumberFunction(random) {
            $("#box_" + random).hide();
            }
            </script>








            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50868840%2fhow-can-i-target-a-box-in-order-to-hide-it-with-jquery%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              Unlike other answers, with each click, a box will be deleted (this is sure to happen) but in other answers may be a few clicks to remove a box.



              $(document).ready(function(){

              var listOfRemovedNumbers =;
              var lengthBoxs = $('[id^=box_]').length;

              function getRandomNumber(){
              do {
              var randomNumber = Math.floor(Math.random() * 10) + 1;
              } while(listOfRemovedNumbers.includes(randomNumber) );
              return randomNumber;
              }

              $('.btn').click(function(){
              var randomNumber = getRandomNumber();
              $('#box_'+randomNumber).remove();
              listOfRemovedNumbers.push(randomNumber);
              if ((listOfRemovedNumbers.length == lengthBoxs ))
              $('.btn').attr("disabled", "disabled").html('All Removed');
              })

              })





              $(document).ready(function(){

              var listOfRemovedNumbers =;
              var lengthBoxs = $('[id^=box_]').length;

              function getRandomNumber(){
              do {
              var randomNumber = Math.floor(Math.random() * 10) + 1;
              } while(listOfRemovedNumbers.includes(randomNumber) );
              return randomNumber;
              }

              $('.btn').click(function(){
              var randomNumber = getRandomNumber();
              $('#box_'+randomNumber).remove();
              listOfRemovedNumbers.push(randomNumber);
              if ((listOfRemovedNumbers.length == lengthBoxs ))
              $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
              })
              })

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
              <div class="container">
              <div class="row">
              <div class="col">
              <div class="container text-center">
              <div id="box_1" class="random">Box 1</div>
              <div id="box_2" class="random">Box 2</div>
              <div id="box_3" class="random">Box 3</div>
              <div id="box_4" class="random">Box 4</div>
              <div id="box_5" class="random">Box 5</div>
              <div id="box_6" class="random">Box 6</div>
              <div id="box_7" class="random">Box 7</div>
              <div id="box_8" class="random">Box 8</div>
              <div id="box_9" class="random">Box 9</div>
              <div id="box_10" class="random">Box 10</div>
              </div>
              </div>
              <div class="col">
              <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
              </div>
              </div>
              </div>








              share|improve this answer



























                up vote
                1
                down vote



                accepted










                Unlike other answers, with each click, a box will be deleted (this is sure to happen) but in other answers may be a few clicks to remove a box.



                $(document).ready(function(){

                var listOfRemovedNumbers =;
                var lengthBoxs = $('[id^=box_]').length;

                function getRandomNumber(){
                do {
                var randomNumber = Math.floor(Math.random() * 10) + 1;
                } while(listOfRemovedNumbers.includes(randomNumber) );
                return randomNumber;
                }

                $('.btn').click(function(){
                var randomNumber = getRandomNumber();
                $('#box_'+randomNumber).remove();
                listOfRemovedNumbers.push(randomNumber);
                if ((listOfRemovedNumbers.length == lengthBoxs ))
                $('.btn').attr("disabled", "disabled").html('All Removed');
                })

                })





                $(document).ready(function(){

                var listOfRemovedNumbers =;
                var lengthBoxs = $('[id^=box_]').length;

                function getRandomNumber(){
                do {
                var randomNumber = Math.floor(Math.random() * 10) + 1;
                } while(listOfRemovedNumbers.includes(randomNumber) );
                return randomNumber;
                }

                $('.btn').click(function(){
                var randomNumber = getRandomNumber();
                $('#box_'+randomNumber).remove();
                listOfRemovedNumbers.push(randomNumber);
                if ((listOfRemovedNumbers.length == lengthBoxs ))
                $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
                })
                })

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                <div class="container">
                <div class="row">
                <div class="col">
                <div class="container text-center">
                <div id="box_1" class="random">Box 1</div>
                <div id="box_2" class="random">Box 2</div>
                <div id="box_3" class="random">Box 3</div>
                <div id="box_4" class="random">Box 4</div>
                <div id="box_5" class="random">Box 5</div>
                <div id="box_6" class="random">Box 6</div>
                <div id="box_7" class="random">Box 7</div>
                <div id="box_8" class="random">Box 8</div>
                <div id="box_9" class="random">Box 9</div>
                <div id="box_10" class="random">Box 10</div>
                </div>
                </div>
                <div class="col">
                <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                </div>
                </div>
                </div>








                share|improve this answer

























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Unlike other answers, with each click, a box will be deleted (this is sure to happen) but in other answers may be a few clicks to remove a box.



                  $(document).ready(function(){

                  var listOfRemovedNumbers =;
                  var lengthBoxs = $('[id^=box_]').length;

                  function getRandomNumber(){
                  do {
                  var randomNumber = Math.floor(Math.random() * 10) + 1;
                  } while(listOfRemovedNumbers.includes(randomNumber) );
                  return randomNumber;
                  }

                  $('.btn').click(function(){
                  var randomNumber = getRandomNumber();
                  $('#box_'+randomNumber).remove();
                  listOfRemovedNumbers.push(randomNumber);
                  if ((listOfRemovedNumbers.length == lengthBoxs ))
                  $('.btn').attr("disabled", "disabled").html('All Removed');
                  })

                  })





                  $(document).ready(function(){

                  var listOfRemovedNumbers =;
                  var lengthBoxs = $('[id^=box_]').length;

                  function getRandomNumber(){
                  do {
                  var randomNumber = Math.floor(Math.random() * 10) + 1;
                  } while(listOfRemovedNumbers.includes(randomNumber) );
                  return randomNumber;
                  }

                  $('.btn').click(function(){
                  var randomNumber = getRandomNumber();
                  $('#box_'+randomNumber).remove();
                  listOfRemovedNumbers.push(randomNumber);
                  if ((listOfRemovedNumbers.length == lengthBoxs ))
                  $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
                  })
                  })

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                  <div class="container">
                  <div class="row">
                  <div class="col">
                  <div class="container text-center">
                  <div id="box_1" class="random">Box 1</div>
                  <div id="box_2" class="random">Box 2</div>
                  <div id="box_3" class="random">Box 3</div>
                  <div id="box_4" class="random">Box 4</div>
                  <div id="box_5" class="random">Box 5</div>
                  <div id="box_6" class="random">Box 6</div>
                  <div id="box_7" class="random">Box 7</div>
                  <div id="box_8" class="random">Box 8</div>
                  <div id="box_9" class="random">Box 9</div>
                  <div id="box_10" class="random">Box 10</div>
                  </div>
                  </div>
                  <div class="col">
                  <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                  </div>
                  </div>
                  </div>








                  share|improve this answer














                  Unlike other answers, with each click, a box will be deleted (this is sure to happen) but in other answers may be a few clicks to remove a box.



                  $(document).ready(function(){

                  var listOfRemovedNumbers =;
                  var lengthBoxs = $('[id^=box_]').length;

                  function getRandomNumber(){
                  do {
                  var randomNumber = Math.floor(Math.random() * 10) + 1;
                  } while(listOfRemovedNumbers.includes(randomNumber) );
                  return randomNumber;
                  }

                  $('.btn').click(function(){
                  var randomNumber = getRandomNumber();
                  $('#box_'+randomNumber).remove();
                  listOfRemovedNumbers.push(randomNumber);
                  if ((listOfRemovedNumbers.length == lengthBoxs ))
                  $('.btn').attr("disabled", "disabled").html('All Removed');
                  })

                  })





                  $(document).ready(function(){

                  var listOfRemovedNumbers =;
                  var lengthBoxs = $('[id^=box_]').length;

                  function getRandomNumber(){
                  do {
                  var randomNumber = Math.floor(Math.random() * 10) + 1;
                  } while(listOfRemovedNumbers.includes(randomNumber) );
                  return randomNumber;
                  }

                  $('.btn').click(function(){
                  var randomNumber = getRandomNumber();
                  $('#box_'+randomNumber).remove();
                  listOfRemovedNumbers.push(randomNumber);
                  if ((listOfRemovedNumbers.length == lengthBoxs ))
                  $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
                  })
                  })

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                  <div class="container">
                  <div class="row">
                  <div class="col">
                  <div class="container text-center">
                  <div id="box_1" class="random">Box 1</div>
                  <div id="box_2" class="random">Box 2</div>
                  <div id="box_3" class="random">Box 3</div>
                  <div id="box_4" class="random">Box 4</div>
                  <div id="box_5" class="random">Box 5</div>
                  <div id="box_6" class="random">Box 6</div>
                  <div id="box_7" class="random">Box 7</div>
                  <div id="box_8" class="random">Box 8</div>
                  <div id="box_9" class="random">Box 9</div>
                  <div id="box_10" class="random">Box 10</div>
                  </div>
                  </div>
                  <div class="col">
                  <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                  </div>
                  </div>
                  </div>








                  $(document).ready(function(){

                  var listOfRemovedNumbers =;
                  var lengthBoxs = $('[id^=box_]').length;

                  function getRandomNumber(){
                  do {
                  var randomNumber = Math.floor(Math.random() * 10) + 1;
                  } while(listOfRemovedNumbers.includes(randomNumber) );
                  return randomNumber;
                  }

                  $('.btn').click(function(){
                  var randomNumber = getRandomNumber();
                  $('#box_'+randomNumber).remove();
                  listOfRemovedNumbers.push(randomNumber);
                  if ((listOfRemovedNumbers.length == lengthBoxs ))
                  $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
                  })
                  })

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                  <div class="container">
                  <div class="row">
                  <div class="col">
                  <div class="container text-center">
                  <div id="box_1" class="random">Box 1</div>
                  <div id="box_2" class="random">Box 2</div>
                  <div id="box_3" class="random">Box 3</div>
                  <div id="box_4" class="random">Box 4</div>
                  <div id="box_5" class="random">Box 5</div>
                  <div id="box_6" class="random">Box 6</div>
                  <div id="box_7" class="random">Box 7</div>
                  <div id="box_8" class="random">Box 8</div>
                  <div id="box_9" class="random">Box 9</div>
                  <div id="box_10" class="random">Box 10</div>
                  </div>
                  </div>
                  <div class="col">
                  <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                  </div>
                  </div>
                  </div>





                  $(document).ready(function(){

                  var listOfRemovedNumbers =;
                  var lengthBoxs = $('[id^=box_]').length;

                  function getRandomNumber(){
                  do {
                  var randomNumber = Math.floor(Math.random() * 10) + 1;
                  } while(listOfRemovedNumbers.includes(randomNumber) );
                  return randomNumber;
                  }

                  $('.btn').click(function(){
                  var randomNumber = getRandomNumber();
                  $('#box_'+randomNumber).remove();
                  listOfRemovedNumbers.push(randomNumber);
                  if ((listOfRemovedNumbers.length == lengthBoxs ))
                  $('.btn').attr("disabled", "disabled").html('All Boxs are Removed');
                  })
                  })

                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
                  <div class="container">
                  <div class="row">
                  <div class="col">
                  <div class="container text-center">
                  <div id="box_1" class="random">Box 1</div>
                  <div id="box_2" class="random">Box 2</div>
                  <div id="box_3" class="random">Box 3</div>
                  <div id="box_4" class="random">Box 4</div>
                  <div id="box_5" class="random">Box 5</div>
                  <div id="box_6" class="random">Box 6</div>
                  <div id="box_7" class="random">Box 7</div>
                  <div id="box_8" class="random">Box 8</div>
                  <div id="box_9" class="random">Box 9</div>
                  <div id="box_10" class="random">Box 10</div>
                  </div>
                  </div>
                  <div class="col">
                  <button class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                  </div>
                  </div>
                  </div>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 21:04









                  halfer

                  14.2k757105




                  14.2k757105










                  answered Jun 15 at 5:36









                  Ehsan

                  9,47031129




                  9,47031129
























                      up vote
                      3
                      down vote













                      Your code was not working fine because you had declared variables box_1, box_2 inside the ready function which were not able outside it i.e. not available in NumberFunction function. Also, you were assigning variables in if rather than comparing. = is assignment and == or === is for comparison.



                      You can improve your code to following:






                      $(document).ready(function() {
                      $(".task_hidden").hide();
                      });

                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>








                      share|improve this answer























                      • It was also not working because he was using = instead of == or ===.
                        – shiftweave
                        Jun 15 at 3:53










                      • @shiftweave - True :)
                        – Nikhil Aggarwal
                        Jun 15 at 3:54






                      • 1




                        Thank you guys!
                        – Alejandro Carvajal Sarria
                        Jun 15 at 5:05










                      • @AlejandroCarvajalSarria - Welcome mate :)
                        – Nikhil Aggarwal
                        Jun 15 at 5:05















                      up vote
                      3
                      down vote













                      Your code was not working fine because you had declared variables box_1, box_2 inside the ready function which were not able outside it i.e. not available in NumberFunction function. Also, you were assigning variables in if rather than comparing. = is assignment and == or === is for comparison.



                      You can improve your code to following:






                      $(document).ready(function() {
                      $(".task_hidden").hide();
                      });

                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>








                      share|improve this answer























                      • It was also not working because he was using = instead of == or ===.
                        – shiftweave
                        Jun 15 at 3:53










                      • @shiftweave - True :)
                        – Nikhil Aggarwal
                        Jun 15 at 3:54






                      • 1




                        Thank you guys!
                        – Alejandro Carvajal Sarria
                        Jun 15 at 5:05










                      • @AlejandroCarvajalSarria - Welcome mate :)
                        – Nikhil Aggarwal
                        Jun 15 at 5:05













                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote









                      Your code was not working fine because you had declared variables box_1, box_2 inside the ready function which were not able outside it i.e. not available in NumberFunction function. Also, you were assigning variables in if rather than comparing. = is assignment and == or === is for comparison.



                      You can improve your code to following:






                      $(document).ready(function() {
                      $(".task_hidden").hide();
                      });

                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>








                      share|improve this answer














                      Your code was not working fine because you had declared variables box_1, box_2 inside the ready function which were not able outside it i.e. not available in NumberFunction function. Also, you were assigning variables in if rather than comparing. = is assignment and == or === is for comparison.



                      You can improve your code to following:






                      $(document).ready(function() {
                      $(".task_hidden").hide();
                      });

                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>








                      $(document).ready(function() {
                      $(".task_hidden").hide();
                      });

                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>





                      $(document).ready(function() {
                      $(".task_hidden").hide();
                      });

                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 15 at 6:37









                      Ehsan

                      9,47031129




                      9,47031129










                      answered Jun 15 at 3:50









                      Nikhil Aggarwal

                      23.1k32647




                      23.1k32647












                      • It was also not working because he was using = instead of == or ===.
                        – shiftweave
                        Jun 15 at 3:53










                      • @shiftweave - True :)
                        – Nikhil Aggarwal
                        Jun 15 at 3:54






                      • 1




                        Thank you guys!
                        – Alejandro Carvajal Sarria
                        Jun 15 at 5:05










                      • @AlejandroCarvajalSarria - Welcome mate :)
                        – Nikhil Aggarwal
                        Jun 15 at 5:05


















                      • It was also not working because he was using = instead of == or ===.
                        – shiftweave
                        Jun 15 at 3:53










                      • @shiftweave - True :)
                        – Nikhil Aggarwal
                        Jun 15 at 3:54






                      • 1




                        Thank you guys!
                        – Alejandro Carvajal Sarria
                        Jun 15 at 5:05










                      • @AlejandroCarvajalSarria - Welcome mate :)
                        – Nikhil Aggarwal
                        Jun 15 at 5:05
















                      It was also not working because he was using = instead of == or ===.
                      – shiftweave
                      Jun 15 at 3:53




                      It was also not working because he was using = instead of == or ===.
                      – shiftweave
                      Jun 15 at 3:53












                      @shiftweave - True :)
                      – Nikhil Aggarwal
                      Jun 15 at 3:54




                      @shiftweave - True :)
                      – Nikhil Aggarwal
                      Jun 15 at 3:54




                      1




                      1




                      Thank you guys!
                      – Alejandro Carvajal Sarria
                      Jun 15 at 5:05




                      Thank you guys!
                      – Alejandro Carvajal Sarria
                      Jun 15 at 5:05












                      @AlejandroCarvajalSarria - Welcome mate :)
                      – Nikhil Aggarwal
                      Jun 15 at 5:05




                      @AlejandroCarvajalSarria - Welcome mate :)
                      – Nikhil Aggarwal
                      Jun 15 at 5:05










                      up vote
                      1
                      down vote
















                      .random {
                      width: 100px;
                      height: 100px;
                      background-color: lightgreen;
                      margin: 30px;
                      }

                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                      <div class="container">
                      <div class="row">
                      <div class="col">

                      <div class="container text-center">

                      <div id="box_1" class="random">Box 1</div>
                      <div id="box_2" class="random">Box 2</div>
                      <div id="box_3" class="random">Box 3</div>
                      <div id="box_4" class="random">Box 4</div>
                      <div id="box_5" class="random">Box 5</div>
                      <div id="box_6" class="random">Box 6</div>
                      <div id="box_7" class="random">Box 7</div>
                      <div id="box_8" class="random">Box 8</div>
                      <div id="box_9" class="random">Box 9</div>
                      <div id="box_10" class="random">Box 10</div>

                      </div>

                      </div>
                      <div class="col">

                      <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                      </div>
                      </div>
                      </div>
                      <script>
                      function NumberFunction(random) {
                      $("#box_" + random).hide();
                      }
                      </script>








                      share|improve this answer

























                        up vote
                        1
                        down vote
















                        .random {
                        width: 100px;
                        height: 100px;
                        background-color: lightgreen;
                        margin: 30px;
                        }

                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                        <div class="container">
                        <div class="row">
                        <div class="col">

                        <div class="container text-center">

                        <div id="box_1" class="random">Box 1</div>
                        <div id="box_2" class="random">Box 2</div>
                        <div id="box_3" class="random">Box 3</div>
                        <div id="box_4" class="random">Box 4</div>
                        <div id="box_5" class="random">Box 5</div>
                        <div id="box_6" class="random">Box 6</div>
                        <div id="box_7" class="random">Box 7</div>
                        <div id="box_8" class="random">Box 8</div>
                        <div id="box_9" class="random">Box 9</div>
                        <div id="box_10" class="random">Box 10</div>

                        </div>

                        </div>
                        <div class="col">

                        <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                        </div>
                        </div>
                        </div>
                        <script>
                        function NumberFunction(random) {
                        $("#box_" + random).hide();
                        }
                        </script>








                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote












                          .random {
                          width: 100px;
                          height: 100px;
                          background-color: lightgreen;
                          margin: 30px;
                          }

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                          <div class="container">
                          <div class="row">
                          <div class="col">

                          <div class="container text-center">

                          <div id="box_1" class="random">Box 1</div>
                          <div id="box_2" class="random">Box 2</div>
                          <div id="box_3" class="random">Box 3</div>
                          <div id="box_4" class="random">Box 4</div>
                          <div id="box_5" class="random">Box 5</div>
                          <div id="box_6" class="random">Box 6</div>
                          <div id="box_7" class="random">Box 7</div>
                          <div id="box_8" class="random">Box 8</div>
                          <div id="box_9" class="random">Box 9</div>
                          <div id="box_10" class="random">Box 10</div>

                          </div>

                          </div>
                          <div class="col">

                          <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                          </div>
                          </div>
                          </div>
                          <script>
                          function NumberFunction(random) {
                          $("#box_" + random).hide();
                          }
                          </script>








                          share|improve this answer















                          .random {
                          width: 100px;
                          height: 100px;
                          background-color: lightgreen;
                          margin: 30px;
                          }

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                          <div class="container">
                          <div class="row">
                          <div class="col">

                          <div class="container text-center">

                          <div id="box_1" class="random">Box 1</div>
                          <div id="box_2" class="random">Box 2</div>
                          <div id="box_3" class="random">Box 3</div>
                          <div id="box_4" class="random">Box 4</div>
                          <div id="box_5" class="random">Box 5</div>
                          <div id="box_6" class="random">Box 6</div>
                          <div id="box_7" class="random">Box 7</div>
                          <div id="box_8" class="random">Box 8</div>
                          <div id="box_9" class="random">Box 9</div>
                          <div id="box_10" class="random">Box 10</div>

                          </div>

                          </div>
                          <div class="col">

                          <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                          </div>
                          </div>
                          </div>
                          <script>
                          function NumberFunction(random) {
                          $("#box_" + random).hide();
                          }
                          </script>








                          .random {
                          width: 100px;
                          height: 100px;
                          background-color: lightgreen;
                          margin: 30px;
                          }

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                          <div class="container">
                          <div class="row">
                          <div class="col">

                          <div class="container text-center">

                          <div id="box_1" class="random">Box 1</div>
                          <div id="box_2" class="random">Box 2</div>
                          <div id="box_3" class="random">Box 3</div>
                          <div id="box_4" class="random">Box 4</div>
                          <div id="box_5" class="random">Box 5</div>
                          <div id="box_6" class="random">Box 6</div>
                          <div id="box_7" class="random">Box 7</div>
                          <div id="box_8" class="random">Box 8</div>
                          <div id="box_9" class="random">Box 9</div>
                          <div id="box_10" class="random">Box 10</div>

                          </div>

                          </div>
                          <div class="col">

                          <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                          </div>
                          </div>
                          </div>
                          <script>
                          function NumberFunction(random) {
                          $("#box_" + random).hide();
                          }
                          </script>





                          .random {
                          width: 100px;
                          height: 100px;
                          background-color: lightgreen;
                          margin: 30px;
                          }

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

                          <div class="container">
                          <div class="row">
                          <div class="col">

                          <div class="container text-center">

                          <div id="box_1" class="random">Box 1</div>
                          <div id="box_2" class="random">Box 2</div>
                          <div id="box_3" class="random">Box 3</div>
                          <div id="box_4" class="random">Box 4</div>
                          <div id="box_5" class="random">Box 5</div>
                          <div id="box_6" class="random">Box 6</div>
                          <div id="box_7" class="random">Box 7</div>
                          <div id="box_8" class="random">Box 8</div>
                          <div id="box_9" class="random">Box 9</div>
                          <div id="box_10" class="random">Box 10</div>

                          </div>

                          </div>
                          <div class="col">

                          <button onclick="NumberFunction(Math.floor(Math.random() * 10) + 1)" class="mt-5 btn btn-outline-danger">Reveal a random box</button>
                          </div>
                          </div>
                          </div>
                          <script>
                          function NumberFunction(random) {
                          $("#box_" + random).hide();
                          }
                          </script>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 15 at 3:54









                          Nandita Arora Sharma

                          9,0272618




                          9,0272618






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50868840%2fhow-can-i-target-a-box-in-order-to-hide-it-with-jquery%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown