Reputation: 7
i have a input field and links with values. when click on a link the value go to the input field. when clicking a other link the value is not count with the value that already is in the input field. example:
<form name="odenen_form">
<input type="text"
class="input_text"name="odenen_miktar"id="odenen_miktar"
style="position:absolute;left:100px;top:50px;width:200px">
</form>
<a onClick="document.odenen_form.odenen_miktar.value+='10'">10</a>
<a onClick="document.odenen_form.odenen_miktar.value+='20'">20</a>
Now when i click on the first link the input field shows me 10 when i click again on the first link the input field shows 1010
Wat i want is 20 as result
Upvotes: 0
Views: 512
Reputation: 115282
Try this,
<form name="odenen_form">
<input type="text" class="input_text"name="odenen_miktar"id="odenen_miktar" style="position:absolute;left:100px;top:50px;width:200px"/>
</form>
<a onClick="var a=document.odenen_form.odenen_miktar;a.value=a.value?parseInt(a.value)+10:10">10</a>
<a onClick="var a=document.odenen_form.odenen_miktar;a.value=a.value?parseInt(a.value)+20:20">20</a>
Upvotes: 0
Reputation: 1930
Seeing as you tagged jQuery, here is a nicely formatted jQuery example:
http://jsfiddle.net/Milanzor/gZ7ZG/
<form name="odenen_form">
<input type="text"
class="input_text"name="odenen_miktar"id="odenen_miktar"
style="position:absolute;left:100px;top:50px;width:200px" />
</form>
<a>10</a>
<a>20</a>
$(document).ready(function(){
$("a").click(function(e){
e.preventDefault();
//The value to add: parseInt is what you want
var toAdd = parseInt($(this).text());
//The current value
var currentVal = $("#odenen_miktar").val();
//Check if its not empty or if its not a number
if(currentVal == '' || parseInt(currentVal) == 'NaN'){
var currentNumber = 0;
}else{
var currentNumber = parseInt($("#odenen_miktar").val());
}
//The new value
var newValue = toAdd + currentNumber;
//Put the new value in the input field
$("#odenen_miktar").val(newValue);
});
});
Upvotes: 0
Reputation: 104805
You're doing string concatenation rather than math. "10"
is not the same as 10
-- also, parse your input
!
<a onClick=" parseInt(document.odenen_form.odenen_miktar.value) += 10 ">10</a>
Upvotes: 1