I got an input type image with a javascript onclick event attached to it. Everytime I click the image it submits the form. I've already added the "return false", buts that that doesn't solve the problem. Here is my code:
<input type="image" onclick="incrementValue(@i)" value="+" />
Javascript
function incrementValue(n) {
var id = 'txtAantal' + n;
var value = parseInt(document.getElementById(id).value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById(id).value = value;
var totaal = parseFloat(document.getElementById('txtTotaal').value, 10);
var prijs = parseFloat(document.getElementById('txtPrice' + n).value, 10);
document.getElementById('txtTotaal').value = totaal + prijs;
return false;
}
I got an input type image with a javascript onclick event attached to it. Everytime I click the image it submits the form. I've already added the "return false", buts that that doesn't solve the problem. Here is my code:
<input type="image" onclick="incrementValue(@i)" value="+" />
Javascript
function incrementValue(n) {
var id = 'txtAantal' + n;
var value = parseInt(document.getElementById(id).value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById(id).value = value;
var totaal = parseFloat(document.getElementById('txtTotaal').value, 10);
var prijs = parseFloat(document.getElementById('txtPrice' + n).value, 10);
document.getElementById('txtTotaal').value = totaal + prijs;
return false;
}
Share
Improve this question
asked Aug 29, 2014 at 23:45
BoussBouss
2052 gold badges9 silver badges22 bronze badges
1
-
You need to “pass on” the return value:
onclick="return incrementValue(@i)"
– C3roe Commented Aug 29, 2014 at 23:51
3 Answers
Reset to default 4Try this:
<input type="image" onclick="return incrementValue(@i)" value="+" />
Try returning false after running the function:
<input type="image" onclick="incrementValue(@i); return false;" value="+" />
<button type="button" onclick="incrementValue(@i)" value="+" >
<img border="0" src="print.png" width="32" heigth="32" title="Click Me"/>
</button>
You should use a button and use type attribute like this