floating field - Post ID 273192

User 1982097 Photo


Registered User
72 posts

I am using the script below, which you advised me on, to add up the item totals on my webform.
One issue is that if a user deletes a number in an item field, it will not adjust the total until a new number is entered or a "0". Is there a way to change the script to recognize the deletion of a number as "0"?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script>var jQ = $.noConflict(true);
// version 1
jQ( document ).ready(function() {
// Set Total_Items_Entered2 to read only
jQ('input[name="Total_Items_Entered2"]').prop('readonly', true);
// add the class sum to all inputs that we want to add
jQ('input[name^="GB"]').addClass( "sum" );
// on keyup check that the value isn't blank and it's numeric
jQ('input[name^="GB"]').on('keyup', function() {

if (jQ(this).val()!="" || jQ.isNumeric(jQ(this).val())){
// if input value is good fire the function
calc_dues();
}
});

// This is the calculate function
function calc_dues() {

var sum = 0;

jQ('.sum').each(function(){

if(jQ.isNumeric(this.value)){

sum += parseFloat(this.value);

jQ('input[name="Total_Items_Entered2"]').val(sum);

}
});
}
});</script>
Coffee roasting is my passion - seriously
User 187934 Photo


Senior Advisor
20,190 posts

Drop the if statement.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script>var jQ = $.noConflict(true);
// version 1
jQ( document ).ready(function() {
// Set Total_Items_Entered2 to read only
jQ('input[name="Total_Items_Entered2"]').prop('readonly', true);
// add the class sum to all inputs that we want to add
jQ('input[name^="GB"]').addClass( "sum" );
// on keyup run calc_dues
jQ('input[name^="GB"]').on('keyup', function() {
calc_dues();
});
// This is the calculate function
function calc_dues() {
var sum = 0;
jQ('.sum').each(function(){
if(jQ.isNumeric(this.value)){
sum += parseFloat(this.value);
jQ('input[name="Total_Items_Entered2"]').val(sum);
}
});
}
});</script>

I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 1982097 Photo


Registered User
72 posts

awesome. thanks!
Coffee roasting is my passion - seriously
User 1982097 Photo


Registered User
72 posts

Thanks for that! another question.
In my previous version of webformbuilder, when i entered a "placeholder" in a field and made it readonly, the placeholder data would populate the database field. In this version it does not. Is there a way to change it so it does?
Coffee roasting is my passion - seriously
User 187934 Photo


Senior Advisor
20,190 posts

Will the input still be readonly?
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 1982097 Photo


Registered User
72 posts

Yes it will.
Coffee roasting is my passion - seriously
User 187934 Photo


Senior Advisor
20,190 posts

You should be able to add the value through jquery and your calculation script should clear it out if it's used.
Place this under your jQ(document).ready(function() {
jQ('input[name="my_input_name"]').val("0");
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.