0

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

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

This works thanms but and when i use 10.99 is not working shows only 10
Use parseDouble for doubles!
0

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);

    });

});

1 Comment

when i use parseFloat its only count before the point. example 10.99 + 10.01 = 20.99 and not 21
0

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>

FIDDLE DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.