Click to See Complete Forum and Search --> : New to Javascript
billswann
04-16-2007, 03:27 PM
Hello,
I am new to javascript but have a problem that I haven;t been able to find an answer for. I have the line in a script that was written by someone else that is no longer avaiable. That line is frm.attachEvent("onsubmit", extrapolate); I would like that line to be condition only on a signle button instead of any button being clicked.
Thank you in advance for any suggestions. :confused:
toicontien
04-16-2007, 04:06 PM
We need to see the full HTML and JavaScript for the page. The line of javascript you gave us is very ambiguous.
billswann
04-17-2007, 06:21 AM
This is the javascript
//==================================================================================================
// CRTTEXTAREA: concatenate a textarea from multiple 25 char fields
// parent: container element in which to place the textarea
// frm: form containing fields for textarea
// fields: array of field names
//==================================================================================================
function crtTextArea(parent, frm, fields) {
// Begin constructing text string from fields
var textout = "";
var trimtextout = ""; // stores version trimmed of trailing line feeds
var lastLineExtraSpace; // count of extra space on last line
for (var n in fields) {
if (frm[fields[n]]) {
// Add contents of field values to string
var txt = frm[fields[n]].value;
// If first word of new text wouldn't fit on the previous line,
// assume text is meant to wrap
var words = txt.split(' ');
if (n == 0) {
// First line; neither space nor newline
} else if (words && words[0] && words[0].length > lastLineExtraSpace) {
// No line feed; text should wrap
txt = " " + txt;
} else {
// Otherwise insert line feed before new text
txt = "\n" + txt;
}
textout += txt;
if (txt != "")
trimtextout = textout;
lastLineExtraSpace = 25 - txt.length - 2;
} else {
// Break out of loop when no more fields found
fields.length = n;
break;
}
}
// Create text area to fit correct number of fields
largeFld = document.createElement("textarea");
largeFld.rows = n;
largeFld.cols = 25;
largeFld.innerText = trimtextout;
parent.appendChild(largeFld);
//==================================================================================
// Put TEXTAREA back into separate 25 char fields when specified buttons are clicked
//===================================================================================
var extrapolate = function(){
// Create pattern object and result array and counter
pattern = /([^ ]{1,25} ?)/g;
var b = new Array("");
var j = 0;
// First break field value into lines
var lines = largeFld.value.split('\n');
for (var lnum in lines) {
var line = lines[lnum];
b[j] = "";
// Match each line against the pattern, which breaks
// the line into words of at most 25 chars
a = line.match(pattern);
if (a) {
for (i=0; i<a.length; i++) {
// If word will not fit on line, place
// word on next line
if(b[j].length + a[i].length > 25) {
j++;
b[j] = "";
}
// Add word to line
b[j] = b[j] + a[i];
}
// Advance to next line
j++;
}
}
// Blank remaining line fields
for (;j<fields.length;j++) {
b[j] = "";
}
// Assign items in array b to form fields
j = 0;
for (var n in fields) {
frm[fields[n]].value = b[j];
j++;
}
}
//==============================
// END EXTRAPOLATE
//==============================
// Attach extrapolate function to fire when form is submitted
frm.attachEvent("onsubmit", extrapolate);
}
//==============================
// END CRTTEXTAREA
//==============================
billswann
04-17-2007, 06:22 AM
Added HTML in attachment in preivous reply.
theRatWonder
04-17-2007, 06:30 AM
As it's an "onsubmit" handler it can only apply to the whole form.
If you want it just for one button it would have to be an "onclick" handler:
document.getElementById(<buttonId>).attachEvent('onclick',extrapolate);