1

here is my code the modal does not display. I want to add all of these attributes(total_cost, J, D, E) on my modal form. It's like generating a report.
I want to use the same button for calling calcost function and display modal.

function CalCost(a) {
  var area = a;

  var J = 0;
  var D = 0;
  var E = 0;
  var P = 0;

  var Total_Cost = 0;

  var max = 100;

  if (a == 1) {
    J = 4250;
    D = 1275;
    E = 1700;
    P = 6375;

    Total_Cost = J + D + E + P;
  } else if (a > 1) {
    var JD = 10 * a;
    J = JD * max;

    var DD = 3 * a;
    D = DD * max;

    var ED = 4 * a;
    E = ED * max;

    var PD = 15 * a;
    P = PD * max;

    Total_Cost = J + D + E + P;

    // window.prompt(" Cost : ",Total_Cost);
    document.getElementById('calValue').innerHTML = Total_Cost;

  }
}

CalCost(2)
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("Cost");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
  modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">
  <div id="myModal" class="modal">
    <div class="modal-content">
      <span class="close">&times;</span>
      <p id='calValue'></p>
    </div>
  </div>
</div>
George
  • 6,630
  • 2
  • 29
  • 36
Sarah Salar
  • 193
  • 1
  • 3
  • 14

1 Answers1

1

Well, As i can see in above snippet code, button was missing. below is the working code.

As i understood you wanted to show the model and call CalCost function on same control click.

Below is the working code for this.

Note- I also added to a textbox which will accept the value dynamically which is being used in CalCost function.

function CalCost(a) {
  var area = a;

  var J = 0;
  var D = 0;
  var E = 0;
  var P = 0;

  var Total_Cost = 0;

  var max = 100;

  if (a == 1) {
    J = 4250;
    D = 1275;
    E = 1700;
    P = 6375;

    Total_Cost = J + D + E + P;
  } else if (a > 1) {
    var JD = 10 * a;
    J = JD * max;

    var DD = 3 * a;
    D = DD * max;

    var ED = 4 * a;
    E = ED * max;

    var PD = 15 * a;
    P = PD * max;

    Total_Cost = J + D + E + P;

    // window.prompt(" Cost : ",Total_Cost);
    document.getElementById('calValue').innerHTML = Total_Cost;

  }
}


// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("Cost");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
var costFactor = document.getElementById("CostInput").value;
alert("Cost Factor is :- " + costFactor);
  CalCost(costFactor)
  modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
<div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'" style="overflow:hidden;">
  <div id="myModal" class="modal">
    <div class="modal-content">
      <span class="close">&times;</span>
      <p id='calValue'></p>
    </div>
  </div>
</div>

<button id="Cost">Show Cost</button> <input id="CostInput" type="text" name="cost" value=2>

Hoping this will help you :)

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
  • thanks for this. kindly review my question here http://stackoverflow.com/questions/43800335/textbox-does-not-active-by-clicking-on-radio-button i do not understand what i am doing wrong here. – Sarah Salar May 06 '17 at 10:56
  • any luck? i am stuck here – Sarah Salar May 08 '17 at 03:32
  • i want to pass variable name area in the function. but it is not working – Sarah Salar May 08 '17 at 04:46
  • 1
    there are multiple way to pass the area.. if this is a fixed number pass that from here.. otherwise dont pass anything from here and calculate this area value in your "CalCost" function.... – Vikash Pandey May 08 '17 at 07:01
  • 1
    thanks yes i did it like u said i get value in calcost function. – Sarah Salar May 08 '17 at 08:12
  • glad to help u :) – Vikash Pandey May 08 '17 at 08:25
  • kindly look at this http://stackoverflow.com/questions/43864515/clear-map-zoom-and-geometries-of-feature-layer – Sarah Salar May 09 '17 at 10:22
  • I checked that.. it is not failing anywhere is your attached code.. this is happening because you are accessing "geometry" attribute of a "undefined" element... somwhere in your code you have added xyz.geometry... hoping this will help you... – Vikash Pandey May 10 '17 at 09:45
  • are your creating any project or u r doing this for learning purpose.. if its a project than show me the project so that we can fix the whole package in one go.... – Vikash Pandey May 10 '17 at 09:46