Sumit
Sumit

Reputation: 48

jquery programming computation and calculation

Check this fiddle

http://jsfiddle.net/8FXFE/6/

This code does show the message when a radio button from name="txtNumber" is changed..

Now what i m trying to achieve is making a formula based on the values of name="txtSpace" & name="txtNumber" radio buttons..

My target is that suppose some one clicks on a radio button in txtNumber fields and another option in txtSpace fields there shall be some calculation of those in "output" window. Calculation can be addition of values of selected radio in txtNumber fields txtSpace fields..

but i m stuck in code and not know how can i change it??

Junk code

<div class="textForm">
<input type="radio" name="txtNumber" value="100" checked="checked" />100
<input type="radio" name="txtNumber" value="200" />200
<input type="radio" name="txtNumber" value="500" />500
<input type="radio" name="txtNumber" value="1000" />1000
<input type="radio" name="txtNumber" value="10000" />10000
<input type="radio" name="txtNumber" value="other" />other
<input type="text" name="other_field" id="other_field" onblur="checktext(this);"
/>
</div>

Upvotes: 0

Views: 67

Answers (3)

El Ronnoco
El Ronnoco

Reputation: 11922

I think you just need to build your string?

http://jsfiddle.net/8FXFE/7/

$("#output").text("Changed to " + $("input[name='txtNumber']:checked").val());

or if you want to combine both outputs you could try something like...

http://jsfiddle.net/8FXFE/10/

$("#output")[0].innerHTML ="Changed to " + $(this).val() + "<br/>" + 
  $('input[name="txtSpace"]:checked').val() + ' ' +
    $('input[name="txtNumber"]:checked').val();

..although you don't say what calculation should take place as 'Space 1 * 1000' doesn't make sense and neither would 'RJ * 200' etc... So I have just combined both radio selections.

Upvotes: 0

bipen
bipen

Reputation: 36531

used change() function and :checked.val() to get the value of checked radio button.. and added the value in txtNumber and txtSpace

try this

$(document).ready(function () {
  $("input[name='txtNumber'],input[name='txtSpace']").change(function () {
    var totalsum=parseInt($("input[name='txtNumber']:checked").val()) + parseInt($("input[name='txtSpace']:checked").val());
    $("#output").text("Changed to " + totalsum);

  });
});

fiddle here

Upvotes: 0

XCS
XCS

Reputation: 28137

If I got your question right you want to display an output determined by both checked radio inputs.

$(document).ready(function () {
    console.log("parsed");
    $("input[name='txtNumber'],input[name='txtSpace']").change(function () {
        $("#output").text("Changed to "+$("input[name='txtNumber']:checked").val() + " " +$("input[name='txtSpace']:checked").val()); 
    });
});

http://jsfiddle.net/8FXFE/8/

Upvotes: 1

Related Questions