0

I have written a function to "highlight" the "product-box" that is clicked by adding a red border around it. However, I want users to be able to select up to 3 boxes at once. As of now, my function adds the border to one and does nothing after.

        <div class="col-md-9"><!-- col-md-9 Begin (Category Listed on Top) -->
          <?php

          getpcatpro();

          $get_products = "select * from products order by rand() LIMIT 0,9";
          $run_products = mysqli_query($con,$get_products);

          while($row_products=mysqli_fetch_array($run_products)){

            $pro_id = $row_products['product_id'];
            $pro_title = $row_products['product_title'];
            $pro_img1 = $row_products['product_img1'];
            $pro_link = $row_products['product_link'];

            echo "
            <div class='col-md-4 col-sm-6'>
              <div class='product' id='product-box' onclick='highlight();'>
                  <center>
                  <img class='img-responsive' src='admin_area/product_images/$pro_img1' style='margin-top: 5%;'>
                  </center>
                <div class='text'>
                  <center>
                    <a href='$pro_link' style='color: black;'> $pro_title </a>
                  </center>
                </div>
              </div>
            </div>  ";
          }
          ?>

          <script> <!-- Script to add border around journal box on click -->
            function highlight(){
              document.getElementById('product-box').style.border = "1px solid red";
            }
          </script>

        </div>
2
  • IDs must be unique. So since you have getElementById('product-box') I'm assuming that you're duplicating them which, as you saw, won't work. Commented Jul 15, 2019 at 15:55
  • you simply add the $pro_id to the related box id: id='product-box-$pro_id' Commented Jul 15, 2019 at 16:03

2 Answers 2

2

You can pass the DOM element directly into highlight function:

 <div class='product' onclick="highlight(this)">
function highlight(target) {
    target.style.border = "1px solid red";
}


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

3 Comments

Wow, worked perfectly, thanks! Any idea on how to 1. Make them unclickable, 2. Limit it to 3 highlights?
1. You can remove the handler inside the highlight like by adding this line: target.onclick = null; 2. You'll probably have to count that in your render method/while loop and do no add onclick handler when the iterator reaches 3.
Would I just add it under the target.style.border = "1px solid red"; ? I just tried that and it still does not unhighlight them
0

Alternatively, you could use classList.add to add multiple styles to the buttons.

function addOutline(target){
    target.classList.add('add-outline');
}

Here's a fiddle https://jsfiddle.net/ninjasoards/q02kgtud/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.