0

I have a webpage with an input box and a button. If a user inputs some values (12345) an object need to appear below or instead of that form (the input box and the button).

I am handling a value check through and my whole code looks like this:

<form name="form" action="" method="post">
  <input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>

<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
  function proveriKljuc() {
    if (document.getElementById("subject").value == 12345) {
      document.write(
        "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>"
      );
    }
  }

</script>

Currently this code is showing a object in a new window (only a object on the page).

Also this is offtopic but if you can help, how can I handle if enter is pressed to activate function proveriKljuc()?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Pacijent
  • 139
  • 1
  • 12
  • What is the problem? – u_mulder Dec 10 '16 at 15:35
  • You can NEVER use document.write after page load. It will wipe the page. Instead have div or a span and fill the innerHTML of it or appendChild of a div created using document.createElement. return false onsubmit to stop submission or make the button `type="button"` - alternatively show a hidden div http://stackoverflow.com/questions/31587326/how-to-show-hidden-div-after-hitting-submit-button-in-form – mplungjan Dec 10 '16 at 15:36

2 Answers2

0

Don't use document.write. If you want to replace the current form:

<div id="wrapper">
<form name="form" action="" method="post">
  <input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
</div>

<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc()
{
if(document.getElementById("subject").value == 12345)
{
document.getElementById("wrapper").innerHTML = "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
}
</script>

If you want your object to appear under the form:

<div id="wrapper">
<form name="form" action="" method="post">
  <input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
</div>

<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc()
{
if(document.getElementById("subject").value == 12345)
{
document.getElementById("wrapper").innerHTML += "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
}
</script>
Hydrothermal
  • 4,851
  • 7
  • 26
  • 45
0

You can NEVER use document.write after page load. It will wipe the page. Instead have div or a span and fill the innerHTML of it or appendChild of a div created using document.createElement. return false onsubmit to stop submission or make the button type="button" - alternatively show a hidden div How to show hidden div after hitting submit button in form?

I cannot show a working example because stacksnippets do not support submit

window.onload = function() {
  document.getElementById("form").onsubmit = function() {
    if (this.elements["subject"].value == 12345) {
      document.getElementById("objContainer").innerHTML = "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
    }
    return false; // ignore submit
  }
}
<form id="form" action="" method="post">
  <input type="text" name="subject" id="subject" placeholder="UNESI KOD">
  <button type="submit" style="margin-top:20px">Potvrdi!</button>
</form>
<div id="objContainer"></div>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236