user853575
user853575

Reputation: 121

Calculation issue With JQuery

I know it is very simple.But Still it is not working.I am multiplying a input number with a fixed number,but is not showing the expected result.it always shows the Error message "Please enter some value" even i enter some integer e.g. 6. This is Html Code.

 <input type="text" class="cc"  id="getdata" />
  <div id="result"> <input type="text" id="show" /></div>
   <input type="button" value="calculate" id="calculate" />

This is JQuery Code.

 $(document).ready(function () {
        $("#calculate").click(function () {
            if ($("#input").val() != ''  && $("#input").val() != undefined) {
             $("#result").html("total value is::" + parseInt($("#input").val()) * 5);
            }
            else {
                $("#result").html("Please enter some value");
            }
         });
         });

Any help will be highly appreciated.

Can anyone tell me please how to concatenate all clicked values of different buttons in a textbox?I want to show previous and current clicked value of button in a textbox. Thank you.

Upvotes: 1

Views: 155

Answers (6)

Jorge Loureiro
Jorge Loureiro

Reputation: 458

Checkout this Fiddle

I think that is what you want.

<input type="text" class="cc"  id="getdata" />
<input type="button" value="calculate" id="calculate" />
<div id="result"></div>​

<script>
$("#calculate").click(calculate);
$("#getdata").keypress(function(ev){
    if(ev.keyCode == 13)
        calculate();
});

function calculate()
{
    var $getData = $("#getdata");
    var $result= $("#result");

    if ($getData .val() != ''  && $getData .val() != undefined && !isNaN($getData .val()))
    {
        $result.append((parseInt($getData .val()) * 5) + "<p>");
    }
    else
    {   
        $result.append("Please enter some value<p>");
    }
    $getData .val("").focus();
}
​
</script>

Upvotes: 0

gaurang171
gaurang171

Reputation: 9090

 $(document).ready(function () {
            $("#calculate").click(function () {
                if ($("#getdata").val() != ''  && $("#getdata").val() != undefined) {
                 $("#result").html("total value is::" + parseInt($("#getdata").val()) * 5);
                }
                else {
                    $("#result").html("Please enter some value");
                }
             });
             });

Upvotes: 0

Jamiec
Jamiec

Reputation: 136249

if ($("#input").val() != ''  && $("#input").val() != undefined) {

You dont have any field anywhere in your markup with the id input!

I think you intended all the instances of #input in that script to be #getdata, but you should also only read its value once into a variable and use that:

$("#calculate").click(function () {
     var val = $('#getdata').val();
        if (val != ''  && val != undefined) {
         $("#result").html("total value is::" + parseInt(val) * 5);
        }
        else {
            $("#result").html("Please enter some value");
        }
     });

Live example: http://jsfiddle.net/82pf4/

Upvotes: 0

Undefined
Undefined

Reputation: 11429

Do you not mean #getdata? Where is #input?

Replace ("#input") with ("#getdata") in your code.

Check out this fiddle.

$(document).ready(function () {
  $("#calculate").click(function () {
     if ($("#getdata").val() != '' && $("#getdata").val() != undefined) {
         $("#result").html("total value is::" + parseInt($("#getdata").val()) * 5);
     } else {
         $("#result").html("Please enter some value");
     }
  });
});​

Upvotes: 3

Mitya
Mitya

Reputation: 34628

There is no field with the ID input in the HTML you posted, yet your jQuery is looking for one. Perhaps you meant $('#show')

With jQuery issues, ALWAYS suspect the selector before even wondering what else might be wrong. Confirm it actually finds the elements you think it does - never assume.

console.log($('#input').length); //0

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382474

You have no input whose id is "input". The jquery selector #somestring is looking for an element whose id is somestring.

Replace ("#input") by ("#getdata") in your code.

Upvotes: 0

Related Questions