Kinnaz
Kinnaz

Reputation: 3

I need a close button on my javascript boxes

so I am creating an application that will log transactions from customers for a café and I am working on my current orders page and there will be boxes on them to present those orders. I am very incompetent at javascript so i am not sure how to add a close button to my boxes when the person is done with the order. Could someone please help me with this. Thankyou. This is my code

#mydiv {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
}

<div id="mydiv">
  <div id="mydivheader">Order Num#</div>
  <p>Order Items</p>
  <span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span>
</div>

<div id="mydiv2">
  <div id="mydivheader">Order Num#</div>
  <p>Order Items</p>
</div>

<div id="mydiv3">
  <div id="mydivheader">Order Num#</div>
  <p>Order Items</p>
</div>

<script>
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;

  }

Upvotes: 0

Views: 80

Answers (2)

Sarath Damaraju
Sarath Damaraju

Reputation: 361

onClick accepts functions. Try creating a function to hide the card. Then call the function on button click.

It could be done more easily with remove.

e.g..

function orderCompleted(event) {
  event.target.parentElement.classList.add('hide-card')
}
.card {
  border: 1px solid #333;
  margin: 5px;
  padding: 5px;
}

.hide-card {
  display: none;
}
<div id="card-1" class="card">
  <h3>Order Number: 123</h3>
  <ul>
    <li>Burger</li>
    <li>Diet coke</li>
  </ul>
  <button onclick="orderCompleted(event)">Completed</button>
</div>

<div id="card-2" class="card">
  <h3>Order Number: 124</h3>
  <ul>
    <li>Pizza</li>
    <li>Hot chocolate</li>
  </ul>
  <button onclick="orderCompleted(event);">Completed</button>
</div>

Above will hide the card from the current view. I hope you are not using static data for the orders (.json). In such case, every time you reload the page, hidden cards will re-appear as we are not updating the order as completed. If you don't see this in the scope of what you are doing, please ignore it.

Upvotes: 1

Hardik Masalawala
Hardik Masalawala

Reputation: 1066

I have added my code between as follow I hope it'll fulfill your requirement

//START UPDATED CODE

//END UPDATED CODE

//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv"));
//START UPDATED CODE
function removeElement(DivId) {
     document.getElementById(DivId).remove();
}
//END UPDATED CODE


function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;

  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv2"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}

//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv3"));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
#mydiv {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
}

#mydiv2 {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
}

#mydiv3 {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
}
#mydivheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
}
<html>
  <head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
  <!-- START UPDATED CODE -->
  <script  src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="  crossorigin="anonymous"></script>
  <!-- END UPDATED CODE -->
  <link rel="stylesheet" type="text/css" href="css_stylesheet.css">
  </head>
  <body>
<div id="tabs" ul class="nav nav-pills">
      <li><a data-toggle="pill" href="#current_orders">Current Orders</a></li>
      <li><a data-toggle="pill" href="#new_order">New Order</a></li>
      <li><a data-toggle="pill" href="#past_orders">Past Orders</a></li>
</div>
<div id="mydiv" class="orderBox">
  <div id="mydivheader">Order Num#</div>
  <p>Order Items</p>
  <!-- START UPDATED CODE -->
  <span id='close' onclick="removeElement('mydiv')"  style="cursor: pointer;" >x</span>
  <!-- END UPDATED CODE -->
</div>

<div id="mydiv2"  class="orderBox" style="top: 25px; left: 169px;">
  <div id="mydivheader">Order Num#</div>
  <p>Order Items</p>
  <!-- START UPDATED CODE -->
  <span id='close' onclick="removeElement('mydiv2')"  style="cursor: pointer;">x</span>
  <!-- END UPDATED CODE -->
</div>

<div id="mydiv3"  class="orderBox" style="top: 23px; left: 378px;">
  <div id="mydivheader">Order Num#</div>
  <p>Order Items</p>
  <!-- START UPDATED CODE -->
  <span id='close' onclick="removeElement('mydiv3')"  style="cursor: pointer;" >x</span>
  <!-- END UPDATED CODE -->
</div>
</body>
</html>

Upvotes: 0

Related Questions