Checkbox, but Radio Button

No worries!

Thanks for that, but unfortunately I still don’t quite get what kind how those values relate to each other regarding sum vs. range. Here’s how you might get the summed prices of each form group (or the entire form) generically though, maybe that helps:

function getPrice (selector) {
  return $(selector)
    // Find all checked price elements within the container
    .find('[data-price]:checked')
    // Map them to their given prices
    .map(function () {
      return parseFloat(this.dataset.price)
    })
    // Get an array from the jQuery collection
    .get()
    // And sum the values
    .reduce(function (a, b) {
      return a + b
    }, 0)
}

$('form').on('change', function () {
  var servicesPrice = getPrice('.services')
  var licensesPrice = getPrice('.license')
  var totalPrice = getPrice('form')

  $('.price-services').text(servicesPrice)
  $('.price-license').text(licensesPrice)
  $('.price-total').text(totalPrice)
})

Here’s a pen.

1 Like

In the causation of learning I wanted something like this →

No sum for now. Just price update based on radio selection.

I was able to get it work later in the above code pen.

I was also able to do it with data-value → separate pen:

Here is teh combo of what i did today:

How can I add sum of the both the operations → Radio + Checkbox ?

You might sum up not only the values of the $(.checkbox)es and $(.radio)s separately, but of all $(.checkbox, .radio)s combined – or the $(:checked) elements of a common container… see my pen above which does exactly that.

1 Like

Yes, I studied that Pen. Slightly tricky(beginners perspective), but code is very concise and crisp. Thanks.

Sir, would it be possible that my Pen can also be fixed and get the total the way I have done it, in case if you have some time to invest.

What part are you having trouble with?

  • Setting up the change event?
  • Getting the first subtotal?
  • Getting the second subtotal?
  • Adding them together?
  • Showing the total?
1 Like

Accomplished.

Stuck here.

If I can add them showing total won’t be a difficulty.

Okay. The values from the page are received as text.

The usual problem when adding numbers when they are text, such as “12” and “34” is that they end up being added to be “1234”.

A good way to fix that is to make them numbers instead, using Number(“12”) + Number(“34”) which gives 46.

Is that similar to the problem that you are facing?

1 Like

Let me try. I will update you.

Just as a passing remark, you’re actually already doing this for the checkboxes – just not for the radios:

$('.checkbox').on('change', function () {
  // var checkticked = [];//array for checkboxes
  var price = 0
  $('.checkbox').each(function () {
    // checkticked.push($(this).attr('data-link'));//get checkboxes
    if ($(this).is(':checked')) {
      price = price + parseInt($(this).attr('data-price'))
    }
    // sum of all price values of this into price
    $('.price2').html(price)
  })
})

… so if you get the price from the checked .radio element in the same way, you might also add both prices to a global total variable or something; alternatively, you might loop over the checkboxes and radios together using a selector list and output the combined result.

Now either way your code is going to be a bit repetitive, so what I did in my pen is abstract away the summing to a dedicated function. Sorry if I was getting carried away with the refactoring… yes that can certainly be done using your code as well. But yeah better give it a try yourself first. :-)

1 Like

So this for radio part:

    $('.radio').on("change", function() {
        // $(".radio") // select the radio by its id
    // .change(function(){ // bind a function to the change event
        if( $(this).is(":checked") ){ // check if the radio is checked
            var val = $(this).val(); // retrieve the value
            $(".price").html(val); //Print the value
        }
    // });
  });
  
  

I finally get this:

$( document ).ready(function() {
    $('.radio').on("change", function() {
        // $(".radio") // select the radio by its id
    // .change(function(){ // bind a function to the change event
        if( $(this).is(":checked") ){ // check if the radio is checked
            var val = $(this).val(); // retrieve the value
            $(".price").html(val); //Print the value
        }
    // });
  });
  
  
   $('.checkbox').on("change", function() {
        // var checkticked = [];//array for checkboxes
        var price = 0;
        $('.checkbox').each(function() {
            // checkticked.push($(this).attr('data-link'));//get checkboxes
            if($(this).is(':checked')){
               price = price + parseInt($(this).attr('data-price'));
            }
            //sum of all price values of this into price
            $('.price2').html(price);
        });        
    });
  var sum = price + val;
  $('.totalprice').html(sum);
});

But this is not calculating the sum → var sum = price + val;

What is it doing instead?

Can you please supply a link to where we can experience what it does instead?

1 Like

Yes, → https://codepen.io/codeispoetry/pen/KKpOEaK

Thanks.

Before we do any interaction with the page, the browser console reports some errors. Please investigate those first.

2 Likes

Hi there, I am unable to get rid of them. I still feel problem is I am not on the right tack of getting the sum.

Well? It’s telling you that the problem is in your pen at line 60, column 13.

What is the code around that area, that involves price?

1 Like

Can you click on the pen.js:60 to see where it takes you?

1 Like