Repeat function (NEWBIE)

K

knoak

Hi,

I have a page with a form, with a javascript-
file included.

I want to change the first letter of some fields
to a capital (and ot with css).

I have this function:
____________________________________________________

function doFields()
{
<!--
var str = document.SendContact.name.value;

// start in all lower case
str=str.toLowerCase()

// first Letter will always be a capital
str = str.substr(0,1).toUpperCase() + str.substr(1,str.length-1)

// capitalize each character after a space
for (i=0; i<str.length; i++) {
if (str.substr(i,1)==" ") {
str = str.substr(0,i+1) + str.substr(i+1,1).toUpperCase() +
str.substr(i+2,str.length-i)
}
}

document.SendContact.name.value = str;
//-->

};
____________________________________________________

This works fine, but there are about 10 fields that
need to be cappitalized. I don't want to write this function
10 times, but do something like

doFields(email);

Hope you understand..

Greetings knoakske
 
I

Ivo

I want to change the first letter of some fields
to a capital (and ot with css).

I have this function:
____________________________________________________

function doFields()
{
<!--

You don't need this commenting out. For legacy purposes (some *very* old
browsers don't know the script tag) some people still use it, but for the
whole script block, never inside a function.
var str = document.SendContact.name.value;

// start in all lower case
str=str.toLowerCase()

// first Letter will always be a capital
str = str.substr(0,1).toUpperCase() + str.substr(1,str.length-1)

// capitalize each character after a space
for (i=0; i<str.length; i++) {
if (str.substr(i,1)==" ") {
str = str.substr(0,i+1) + str.substr(i+1,1).toUpperCase() +
str.substr(i+2,str.length-i)
}
}

document.SendContact.name.value = str;
//-->

};
____________________________________________________

This works fine, but there are about 10 fields that
need to be cappitalized. I don't want to write this function
10 times, but do something like

doFields(email);

Your function can receive the argument like this:
function doFields(str){
}
and str will equal email. Then you no longer need the line that starts with
"var str=". If email is not the string value but the name of the form
element, change that line to
str=str.value;
or call doFields(email.value);
HTH
Ivo
 
M

Mick White

knoak said:
Hi,

I have a page with a form, with a javascript-
file included.

I want to change the first letter of some fields
to a capital (and ot with css).

I have this function:
____________________________________________________

Why not pass the name of the field to the function?
function doFields(field){
var str = field.value.toLowerCase();
str = str.substring(0,1).toUpperCase() + str.substring(1)

...........


<input type="text" name="foo" id="foo" onchange="doFields(this)" />

Mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top