0

function dateselecter(){
    var d=document.getElementById("selecter").value;
    alert(d);
    var dates=['monday','Tuessday', 'Wendnesday','Thursday', 'Friday', 'Saturday', 'Sunday'];
    // document.write("im good");
    var date=parseInt(d);
    // alert(date);
    var day=(dates[date-1]);
    // alert(day);
    document.getElementById("day").innerHTML=day;
<body>
    <form method="post">
    <label>Select a number</label>
        <select id='selecter'>
          <option id="items" value="1">1</option>
          <option id="items" value="2">2</option>
          <option id="items" value="3">3</option>
          <option id="items" value="4">4</option>
          <option id="items" value="5">5</option>
          <option id="items" value="6">6</option>
          <option id="items" value="7">7</option>
        </select>
   <button type="submit" onclick=dateselecter()>submit</button>
</form>
<h3 id="day"></h3>
</body>

When i hit the submit button the name showing on screen like less than second.how to keep it on the screen?

  • `type="submit"` The button is submitting the form to the server. – Heretic Monkey Nov 03 '20 at 14:49
  • Does this answer your question? [How to prevent buttons from submitting forms](https://stackoverflow.com/questions/932653/how-to-prevent-buttons-from-submitting-forms) – Heretic Monkey Nov 03 '20 at 14:51
  • Does this answer your question? [How to prevent form from being submitted?](https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) – KiraLT Nov 03 '20 at 14:51

1 Answers1

0

The issue is because you submit the form on button click (because it has type: "submit").

You can add return false to onclick to disable default behavior:

<button type="submit" onclick="dateselecter(); return false;">submit</button>

You can also disable form submit:

<form onsubmit="return false;" >

Finally, you can change the button type from submit to button.

KiraLT
  • 2,385
  • 1
  • 24
  • 36