Calculate Total Cost Based on Word Count
Posted: Fri Dec 12, 2008 11:38 am
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:
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 += " 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;
}