Hey guys,

I have a code on excel, that will count the critera if there is more than one box filled out.

=COUNTIF(C5:D14,"<>0")

But now i am converting this code to jquery and i cannot figure it out.
on my page, there is a toggle button, yes/no selector for autopay,

<td><input name="autopay" type="radio" value="y" />Y<input type="radio" name="autopay" value="n" />N</td>

The input fields are labelled as such:

<td><input type="text" name="laccess1" id="laccess1" class="form-control" value="0.00" /></td>

<td><input type="text" name="laccess2" id="laccess2" class="form-control" value="0.00" /></td>

I am trying to get it to count them if they have a value in it, like 25.00 or more and when the toggle event happens, I want it to take 5.00 off per box that has a value in it. I am going to have a total of 10 of these boxes that can be used.

The code I am going to use for the toggle event is:

$( "#autopay" ).toggle(
  function() {
    $( '#overallTotal' ).val - (amount of inputs 
    that meet the critera * 5);
    }, 
);

Recommended Answers

All 14 Replies

Member Avatar for diafol

So every time you toggle you pull 5 from each box. Presumably until you hit 0.00 again? You don.t want negative values?

Kinda, When i toggle the auto pay to yes, i want it to take 5.00 from the laccess(1-10) box, then i toggle no, it puts the 5.00 back. So, if there is a value of 25.00 it will be 25 if it is no, and 20 if yes.

Member Avatar for diafol

Ok understood

As it stands, the question is not 100% self-explanatory, in particular the relationships and juxtapositions of the various input elements.

Maybe it would be clearer if you posted an (annotated) screenshot of the original spreadsheet and/or the HTML version.

Member Avatar for diafol

In addition, how are you storing this info? DB? Are you sending this form anywhere? Or is it just a practice form?

Member Avatar for diafol

Also - what if you change the amount in the textbox after you've clicked a radiobutton - it would cause problems?

So here is the tracker, its an excel sheet, By the activation fee text is a field (cell o16) with the count if, which counts the amount that have text greater than 0, and then the formula will take the amount of lines and subtracts it after it times the formula by 5.

i modeled the site to look just like it.
Line_access2.png

Diafol, to answer your question there are going to be multiple things that can be used by this, for example,
VZW has a line access of 15.00 with out autopay, it stays 15.00, with autopay, it will change to 10.00...
where as Spr, has a line access of 25.00 with out autopay and with autopay its 20.00

WAIT,

can i just have a formula that takes the div id's stating that if 0.00 or null, to leave alone, at 0.00 anything >0 remove 5.00 if autopay is yes?

like:

function autopay.toggle(){
var laccess1 = parseFloat($('#laccess1').val());
var autopay = this.toggle('#autopay');
if statement{
autopay = 'yes"{
var output = laccess1-5 ;
}else{
var output = laccess1;
return (isNaN( output )) ? 'ERROR' : output.toFixed(2); 
}

Or some thing like that so its much easier?

Patrick_18 is my alt, for some reason my email has 2 tied to it and i am not sure why.

Diafol, to answer the one question that i missed, there is no database, its all user input.

Would something like this work?

$('#myForm input').on('change', function() {
  $('#laccess1').val(autopay());
});

function autopay() {

  var laccess1 = parseFloat($('#laccess1').val());
  var my_radio = $("input[name='autopay']:checked").val();

  if (my_radio == 'y') {

    var output = laccess1 - 5;

  } else {

    var output = laccess1 + 5;
  }

  return (isNaN(output)) ? 'ERROR' : output.toFixed(2);
}
Member Avatar for diafol

Is Autopay a general property of the whole sheet or is it applied per line?
IMO, it is better to leave the actual amount the same in the textbox, but then have a label (non-editable) with the corrected amount (or a label with a "discount" heading).

A checkbox may be a better fit than a Yes/No.

Is Autopay a general property of the whole sheet or is it applied per line?
IMO, it is better to leave the actual amount the same in the textbox, but then have a label (non-editable) with the corrected amount (or a label with a "discount" heading).

A checkbox may be a better fit than a Yes/No.

It is applied per line, each line will have a line access of 15,25, or 45 assigned per plan they are using.

EG Spr:
So in line 1,
Device is 26.39, data plan is 30.00, line access for line 1 is 25.00
line 2,
device is 26.39 data plan stays the same, since it was already defined, line access is 25.00

Now with VZW:
Device is 26.39, data plan is 12.50, line access for line 1 is 15.00
line 2,
device is 26.39, data stays the same, line 2 is 15.00

with autopay, the line access for each line is 5.00 cheaper(as a discount) for signing up with autopay.

Member Avatar for diafol

OK, well you can set an event listener for the checkbox click (autopay) - or radiobutton if you prefer. This just enters a 5.00 discount in a div (or disabled textbox) if there is a sufficient amount (at least 5.00) in the laccess textbox if checked = true, clearing the discount box on checked = false. Just run a general totaller function after this. The totaller function should be invoked on each change to the textboxes.

$('#autopay').click(function(){
    if( $(this).prop('checked') ){
        $('#discount').val('5.00');
    }else{
        $('#discount').val('0.00');
    }
    totaller();
});

//give all editable textboxes the "totalize" class
$('.totalize').change(function(){
    //do other stuff?
    totaller();
});

function totaller(){
    //run loop to create totals on all lines
}

That's a barebones approach. I'd probably include a parameter for totaller function to do all lines or just a selected one.

Neither the pictures nor the words are speaking to me.

Until such time that the question is properly expressed, I have to admit defeat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.