Greetings, I am looking for a way to avoid the refresh that my page does when adding an element by pressing a button, either adding, subtracting or deleting an element. I would like it to stay in the same place and not refresh the page since it takes me to the beginning and is uncomfortable. My code is the following:
These are the buttons that do the actions:
<a href="#"><img onclick="add(<?php echo $id ?>,'add');" src="img/sumar.png"></a>
<a href="#"><img onclick="add(<?php echo $id ?>,'remove');" src="img/resta.png"></a>
<a href="#"><img onclick="add(<?php echo $id ?>,'removeProd');" src="img/remove.png"></a>
This is my JS:
function add(valor,valor2){
$.get('add.php',{id:valor,action:valor2},function(data){
$('.producto').html(data);
})
}
function add2(valor,valor2){
$.get('add.php',{id:valor,action:valor2},function(data){
window.location='';
})
}
function add3(valor,valor2,valor3){
$.get('add.php',{id:valor,action:valor2,su:valor3},function(data){
$('.producto').html(data);
})
}
This is what I have, what I want is that pressing any of the buttons does not reload the page, since it takes me to the top of the page again. To whom can help me with this thank you very much, I do not know js well
As it is your code to avoid the
refresh
you would have to do at least two changes, first returnfalse
in your functions, and then add thereturn
inside theonclick
before calling the function.HTML
Js (same, return false to your other functions)