HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Price calculator (quote) based on word count

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
countpricewordbasedquotecalculator

Problem

I am working on an ad form for a local newspaper. I am not familiar with the programming side of things, still learning. I was able to successfully get the form to count the words, but I need it to quote the price based on different packages/ word counts. Here is an example of the form and what the price structure example is. Any help is appreciated to point me in the right direction! Please be nice, this is practice/learning for me!!

One paper $6.50 for 15 words, then 10 cents per word thereafter. Two papers $12.50 for 15 words then 15 cents per word thereafter. Three papers $18.00 for 15 words then 15 cents per word thereafter. Four papers $22.50 for 15 words then 15 cents per word thereafter.





Place An Ad

counter = function() {
var value = $('#text').val();

if (value.length == 0) {
$('#wordCount').html(0);
return;
}

var regex = /\s+/gi;
var wordCount = value.trim().replace(regex, ' ').split(' ').length;


$('#wordCount').html(wordCount);
};

$(document).ready(function() {
$('#count').click(counter);
$('#text').change(counter);
$('#text').keydown(counter);
$('#text').keypress(counter);
$('#text').keyup(counter);
$('#text').blur(counter);
$('#text').focus(counter);
});

First Name

Last Name

Billing Address:

City:

State:

Zip Code:

Classification:

4X4
Acreage
Antiques
Appliances
Apts Duplex For Rent
Auctions
Automobiles
Boats
Business for Sale
Business Opportunity
Childcare
Classic Vehicles
Commercial Rent
Commercial Sale
Computers
Electricians
Employment

Ad Body:

Words: 0

Attach Photo:

Number of Weeks:

1
2
3
4

Extended Coverage Areas:

Coverage area 1 Eastern Washington including Spokane

Coverage area 2 Lewiston and Moscow areas

Coverage area 3 TriCities and surrounding areas

Comments:

Please note: Ads without complete billing information or ad text will not be accepted.

Solution

I'll add a simplification to your doc-ready event listeners. You can write it like this:

$(document).ready(function () {
    $('#count').click(counter);
    $('#text').on('change keydown keypress keyup blur focus', counter);
});


$.on can take multiple events separated by spaces.

Code Snippets

$(document).ready(function () {
    $('#count').click(counter);
    $('#text').on('change keydown keypress keyup blur focus', counter);
});

Context

StackExchange Code Review Q#94015, answer score: 3

Revisions (0)

No revisions yet.