Calculate Total Cost Based on Word Count

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
itsinmyhead
Forum Commoner
Posts: 25
Joined: Wed Aug 13, 2008 11:33 am

Calculate Total Cost Based on Word Count

Post by itsinmyhead »

So, I've got a form setup on our website for folks to place a Classified Ad. Next to the text area is a word count - ads cost a set amount up to a certain number of words, then the cost increases per word after that.

We have about 15 sections which are selectable in a drop down list. I'm looking for a way to calculate the cost for each section once a choice is made and the ad is input into the text area.

If you need it (for gathering the word count totals), here is the Javascript code for that function:

Code: Select all

if (document.getElementById && document.getElementsByTagName && document.createTextNode) 
    window.onload = initWordCount;
 
function initWordCount() {
    for($i = 0; $i < document.getElementsByTagName("textarea").length; $i++) {
        var thisarea = document.getElementsByTagName("textarea")[$i];
        var textareaID = thisarea.id;
        var spanID = textareaID + "_count";
        thisarea.setAttribute("onkeyup", "countwords('" + textareaID + "', '" + spanID + "', 'y')");
        var newwordsp = document.createElement("div");
        var target = thisarea.parentNode;
        target.insertBefore(newwordsp, target.firstChild);
        newwordsp.className = "counter";
        var newcountspan = document.createElement("span");
        newwordsp.appendChild(newcountspan);
        newcountspan.setAttribute("ID", spanID);
        newwordsp.innerHTML += "&nbsp;Words";
        countwords(textareaID, spanID, "y");
    }   
}
 
function countwords(textareaID, destinationID, striphtml) {
  var txt = document.getElementById(textareaID).value;
  if (striphtml == "y") txt = txt.replace(/(<([^>]+)>)/ig,""); //strip HTML
  txt = txt.replace(/\s*((\S+\s*)*)/, "$1"); //ltrim
  txt = txt.replace(/((\s*\S+)*)\s*/, "$1"); //rtrim
  document.getElementById(destinationID).innerHTML = txt.match(/^ *$/) ? 0 : txt.split(/\s+/g).length;
}
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Calculate Total Cost Based on Word Count

Post by kaszu »

As I understand you are sharing, not asking for help.

1) Does it really work? Somehow I don't remember "$i" being a valid variable definition.
Don't do "getElementsByTagName" in each loop.
Initialize variable before using it ("var i = 0").

Code: Select all

for($i = 0; $i < document.getElementsByTagName("textarea").length; $i++) {
        var thisarea = document.getElementsByTagName("textarea")[$i];
should be

Code: Select all

var textareas = document.getElementsByTagName("textarea");
for(var i = 0, j = texareas.length; i < j; i++) {
        var thisarea = textareas[i];
2)

Code: Select all

thisarea.setAttribute("onkeyup", "countwords('" + textareaID + "', '" + spanID + "', 'y')");
Use addEventListener
3)

Code: Select all

if (striphtml == "y")
Since you are expecting only 2 values ("y" or anything else), striphtml should be boolean, not string
itsinmyhead
Forum Commoner
Posts: 25
Joined: Wed Aug 13, 2008 11:33 am

Re: Calculate Total Cost Based on Word Count

Post by itsinmyhead »

No, I was asking for help. Sorry if that got muddled up there.

The code I posted is the code that calculates the number of words entered into the text area. I wasn't sure if someone might need that code in order to point to a variable or anything else in it.

What I am looking for is a way to calculate a total cost.

For instance, let's say we select Automotive as the section we want to post an ad in. It has a flat rate of $4.00 up to 20 words. It is 15 cents for every word after 20. Say there are 22 words typed into the text area. I would like to display the total cost of $4.30 so the customer can see the cost before mailing in their ad (there isn't a payment method setup right now - just e-mailing in ads).
Post Reply