1

I will explain my problem: I have a form and I want to click on submit to display a div (in the same page) and run a java script function without refreshing the page

<form action="#" method="post" name= "form1" id = "form1">
<div class="row_ligne">
  <div class="col-25">
    <label for="fname">Fréquence </label>
  </div>
  <div class="col-75">
    <select id="frequence" name="frequence" onchange="showDiv(this)">
      <option value="yearly">Annuelle</option>
      <option value="monthly">Mensuelle</option>
      <option value="daily">Quotidienne</option>
    </select>
  </div>
</div>

<div class="row_ligne">
      <div class="col-50">

        <input type="submit" id="Backtest" value ="Backtest" name = 
  "Backtest" onclick ="open_div_res(),openOnglet(event, 'repartition');" >

      </div>

     </div>
</form>

<div class="tab_back" id ="id_div_res" style="display:none;">
<button class="tablinks" 
onclick="openOnglet(event,'repartition')">Result</button> </div>

<script>
function open_div_res(){

          document.getElementById("id_div_res").style.display = "block";
          document.getElementById('id_div_res').scrollIntoView();

      }

   </script>
Dipnesh Parakhiya
  • 516
  • 1
  • 5
  • 17
hamza el hadry
  • 33
  • 1
  • 2
  • 6
  • Here it shows how to prevent form from submitting, https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted you only have to get your values and do whatever inside the js function. – Dula wnRp Apr 14 '18 at 10:45

3 Answers3

0

Add type="button" to the button, you don't do this it will trigger a submit.

<button type="button" class="tablinks" 
onclick="openOnglet(event,'repartition')">Result</button>

If you mean by clicking this:

<input type="submit" id="Backtest" value ="Backtest" name = 
 "Backtest" onclick ="open_div_res(),openOnglet(event, 'repartition');" >

You have to change that into a button also

sietse85
  • 1,488
  • 1
  • 10
  • 26
0

If I do understand this correctly, you want to NOT refresh the page when you click on submit.

Add a listener on your submit button, and, using jQuery

$("#mySubmitButton").on("click", function(event){
  event.preventDefault();
  // show the div code
  $("#myDiv").show();
});

Or, vanilla javascript :

document.getElementById('mySubmitButton').addEventListener('click', function(e){ e.preventDefault(); document.getElementById('myDiv').classList.add('isVisible'); });

this should do the trick !

0

Try this:

<form action="#" method="post" name= "form1" id = "form1" onSubmit="return false;">
   //Code here
</form>
Sanjay
  • 540
  • 2
  • 13
  • 29