Forum Moderators: open

Message Too Old, No Replies

One event for all text fields on form

         

Nutter

5:06 pm on Dec 23, 2004 (gmt 0)

10+ Year Member



I have a js routine that allows or disallows certain characters from being entered. Is there a way to have it work on every text box on a form, short of going in to each and putting it in the onKeyPress?

- Ryan

garann

10:31 pm on Dec 23, 2004 (gmt 0)

10+ Year Member



If your server is able to host JSP or ASP.NET pages (and maybe other languages), you can define your own textbox that includes the onKeyPress attribute. But you'd still have to go through and replace every textbox with your custom one, so your server knows to add your code.

orion_rus

8:20 pm on Dec 25, 2004 (gmt 0)

10+ Year Member



Nutter, why don't u make a <input> with onKeyPress event pointing on the spelling function of each input box?

Nutter

12:14 am on Dec 26, 2004 (gmt 0)

10+ Year Member



That's what I've done; or more accurately, am currently doing. I was just hoping there was an easier way than having to do it for all 50 something boxes.

Lance

11:49 am on Dec 26, 2004 (gmt 0)

10+ Year Member



Check out the sixth message in this thread [webmasterworld.com] that I posted a few days ago. Change the <a> element references to <input>s and the oncontextmenu call to onkeypress and you should be weel on your way.

It will attach an onkeypress event to your input tags when in the onload event. No need to hand code each <input> element.

Hope this helps.

Nutter

10:33 pm on Dec 26, 2004 (gmt 0)

10+ Year Member



Lance -
Yes that helps; it looks to be exactly what I was trying to do.

Thanks,
- Ryan

Lance

11:25 pm on Dec 26, 2004 (gmt 0)

10+ Year Member



Sure thing. Let me know if it works for you. If not, perhaps I can help further.

Nutter

12:33 am on Dec 27, 2004 (gmt 0)

10+ Year Member



I got an error: attachEvent not a function, or something like that in FF and IE gave me a different error (don't remember what). I just wound up putting the code in each input box onKeyPress, and it worked. As it turned out, several of the fields were different on what they could and couldn't accept, so it wouldn't have wound up saving that much effort anyway.

But, I appreciate the help; and it's probably something I'll use in the future with links.

- Ryan

adni18

3:06 am on Dec 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why not do this?

<HEAD>
<TITLE>Adding onkeypresses to every textarea.</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
function="alert()";
function eventsAdd()
{
var z=document.getElementsByTagName("textarea");
for(var y=0;y<z.length;y++)
{
z[y].onkeypress=new Function(function);
}
}
//-->
</SCRIPT>
</HEAD>
<BODY ONLOAD="eventsAdd()">

Textarea 1: <textarea></textarea>

<BR>

Textarea 2: <textarea></textarea>
</BODY>