0

I know that this question was asked before, but i didn't find that helpful for my case. I tried

prevent refresh of page when button inside form clicked

The above solution will prevent the refresh but it will not send the Http request.

i have also tried using javascript and it did not work out either.

document.querySelector('#myButton').addEventListener('click', function(event) {
  event.preventDefault();
});

My current code

   <form method="post"> 
                               
   <a href="http://192.xxxxx/aaa/cccccc"><button type="button" class="button2" > Click me</button></a>
                                  
   </form>

Any help is appreciated !!

JTC
  • 119
  • 1
  • 11
  • Since that's not a `type='submit'`, which would be the default if you did not make it `type='button'`, it shouldn't submit. `event.preventDefault()` is used on the `form` itself `onsubmit` to prevent submission. You probably need to clear your cache. – StackSlave Mar 01 '21 at 23:35
  • @stack slave . Thank you for the asnwer, but I tried clearing my cache as well. – JTC Mar 01 '21 at 23:41
  • 1
    You shouldn't put a button inside ``. Clicking on the link reloads the page. – Barmar Mar 01 '21 at 23:48
  • @Barmar is correct. Unless you just have a hashtag in there, it will reload the page. – StackSlave Mar 01 '21 at 23:51
  • Hey @ barmer, i have updated the code, my previous version of the code was wrong. Currently my link is under the href tag and not under action. Is there any other solution, as i need to use the href tag because i use another javascript file to change the value of href. – JTC Mar 02 '21 at 01:09

0 Answers0