2

I'm trying to make the value of an input text show on a pop-up, both on the click of an input button.  For example, if I type "James" and press the button, "James" will show in the pop-up. (EXAMPLES BELOW)

However, the code below isn't working on my site (nor JSFiddle). It does work on the Stack Snippet, rather strangely. How can I fix the code so that it works everywhere?

JSFiddle (not working) - https://jsfiddle.net/23wom1f7/

Stack Snippet (working) -

function preview() {
  var fname = document.getElementById("fname").value;
  document.getElementById("modalname").innerHTML = fname;
}

var modal = document.querySelector(".modal");
var trigger = document.querySelector("#preview");
var closeButton = document.querySelector("#close");

function toggleModal() {
  modal.classList.toggle("show-modal");
}

function windowOnClick(event) {
  if (event.target === modal) {
    toggleModal();
  }
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", toggleModal);
window.addEventListener("click", windowOnClick);
.modal {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0;
  visibility: hidden;
  transform: scaleX(1.1) scaleY(1.1);
  transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  padding: 1rem 1.5rem;
  width: 24rem;
  border-radius: 0.5rem;
}

#close {
  float: right;
  width: 1.5rem;
  line-height: 1.5rem;
  text-align: center;
  cursor: pointer;
  border-radius: 0.25rem;
  background-color: lightgray;
}

#close:hover {
  background-color: darkgray;
}

.show-modal {
  opacity: 1;
  visibility: visible;
  transform: scaleX(1.0) scaleY(1.0);
  transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<input class="formData" id="fname" type="text" maxlength="20" name="fname" placeholder="First Name" autocomplete="off">

<input type="button" class="formAsk" id="preview" onclick="preview()" name="preview" value="PREVIEW">

<div class="modal">
  <div class="modal-content">
    <span id="close">&times;</span>
    <h3>Preview Your Message:</h3> 
    Name: <span id="modalname"></span>
  </div>
</div>
aman.s
  • 86
  • 9
  • For jsFiddle, " You have configured JSFiddle to wrap the code in an onload event handler. Consequently the function you are trying to call onclick is out of scope. " https://stackoverflow.com/questions/9114747/onclick-event-not-firing-on-jsfiddle-net – Md Sifatul Islam Feb 04 '18 at 05:00
  • Possible duplicate of [Onclick event not firing on jsfiddle.net](https://stackoverflow.com/questions/9114747/onclick-event-not-firing-on-jsfiddle-net) – Md Sifatul Islam Feb 04 '18 at 05:01
  • Possible duplicate of [Why does this simple JSFiddle not work?](https://stackoverflow.com/questions/7043649/why-does-this-simple-jsfiddle-not-work) – Sebastian Simon Feb 04 '18 at 08:13

1 Answers1

2

This works: https://jsfiddle.net/zsetxdrg/

basically just changed this....

this.preview = function() {
  var fname = document.getElementById("fname").value;
  document.getElementById("modalname").innerHTML = fname;
}

Looks like a scoping issue, check your console its not seeing the preview() function.

Stradosphere
  • 1,285
  • 3
  • 14
  • 40
  • Okay, thanks! Actually I tried this again, with
    tags surrounding the input, but now it doesn't work! Is there a fix for this? https://jsfiddle.net/23wom1f7/1/
    – aman.s Feb 04 '18 at 16:59