Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Adaptive forms with Javascript

Reply
Thread Tools

Adaptive forms with Javascript

 
 
Damo
Guest
Posts: n/a
 
      05-11-2007
Hi
I'm new to javascript and i'm trying to create a simple form that:
has a text field
and a button
when the page is initially loaded the text field is not visible.
Pressing the button is supposed to make the text field visible

This is the code I have
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<script src = "srcipt.js" type="text/javascript"></script>
<form>
<fieldset id="testField">
<legend>Test</legend>
Test Field &nbsp<input type="text"/>
</fieldset>
<fieldset>
<legend>Test</legend>
<button type ="button" id="buttonfield" >Show field</button>
</fieldset>
</form>
</body>
</html>

The Javasrcript(in an external file)

if(window.attachEvent)
{

window.attachEvent('onload', setupForm);
}
else if(window.addEventListener)
{
window.addEventListener('load', setupForm,true);
}

function setupForm(someEvent)
{

document.getElementById("testField").style.display ="none";

var field = document.getElementById"buttonField");

if(window.attachEvent)
{
field.attachEvent('onclick', handleClick);
}
else if(window.addEventListener)
{
field.addEventListener('click', handleClick,true);
}

}

function handleClick(someEvent)
{

document.getElementById("testField").style.display ="";
}


Its not working and i have no idea why, Can anyone please help? Thanks

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      05-11-2007
Damo said:
>
>Hi
>I'm new to javascript and i'm trying to create a simple form that:
>has a text field
>and a button
>when the page is initially loaded the text field is not visible.
>Pressing the button is supposed to make the text field visible
>
>This is the code I have
> HTML
>
><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
><html xmlns="http://www.w3.org/1999/xhtml">
><head>
><title>Test</title>
><link rel="stylesheet" type="text/css" href="style.css" />
></head>
><body>
><script src = "srcipt.js" type="text/javascript"></script>


Check the name of your script file.


--

 
Reply With Quote
 
 
 
 
Damo
Guest
Posts: n/a
 
      05-11-2007
On May 11, 9:01 pm, Lee <(E-Mail Removed)> wrote:
> Damo said:
>
>
>
>
>
>
>
> >Hi
> >I'm new to javascript and i'm trying to create a simple form that:
> >has a text field
> >and a button
> >when the page is initially loaded the text field is not visible.
> >Pressing the button is supposed to make the text field visible

>
> >This is the code I have
> > HTML

>
> ><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> ><html xmlns="http://www.w3.org/1999/xhtml">
> ><head>
> ><title>Test</title>
> ><link rel="stylesheet" type="text/css" href="style.css" />
> ></head>
> ><body>
> ><script src = "srcipt.js" type="text/javascript"></script>

>
> Check the name of your script file.
>
> --- Hide quoted text -
>
> - Show quoted text -


oops... i changed it but still nothing

 
Reply With Quote
 
scripts.contact
Guest
Posts: n/a
 
      05-11-2007
On May 11, 1:45 pm, Damo <(E-Mail Removed)> wrote:
> if(window.attachEvent)
> {
>
> window.attachEvent('onload', setupForm);}
>
> else if(window.addEventListener)
> {
> window.addEventListener('load', setupForm,true);
>
> }
>
> function setupForm(someEvent)
> {
>
> document.getElementById("testField").style.display ="none";
>
> var field = document.getElementById"buttonField");
>
> if(window.attachEvent)
> {
> field.attachEvent('onclick', handleClick);
> }
> else if(window.addEventListener)
> {
> field.addEventListener('click', handleClick,true);
> }
>
> }
>
> function handleClick(someEvent)
> {
>
> document.getElementById("testField").style.display ="";
>
> }
>



Try this code:

function addListener(ev,lis,elem){
elem=elem||window;
if(window.attachEvent)
elem.attachEvent('on'+ev, lis);
else if(window.addEventListener)
elem.addEventListener(ev, lis,false);
else
elem['on'+ev]=lis
}

addListener("load",setupForm)

function setupForm(someEvent)
{
document.getElementById("testField").style.display ="none";
var field = document.getElementById("buttonField");
addListener('click', handleClick,field);
}

function handleClick(someEvent)
{
document.getElementById("testField").style.display ="";
}

 
Reply With Quote
 
Damo
Guest
Posts: n/a
 
      05-11-2007
excellent , cheers that did the trick.
What does the line,

elem['on'+ev]=lis ;


do?

Thanks

 
Reply With Quote
 
scripts.contact
Guest
Posts: n/a
 
      05-12-2007

Damo wrote:
> excellent , cheers that did the trick.
> What does the line,
>
> elem['on'+ev]=lis ;
>


If the tests for addEventListener and attachEvent fails, it sets the
onEventName attribute of element.

For example-if you call
addListener("load",someFunc)

then that line(if it runs) will set:
window.onload=someFunc

 
Reply With Quote
 
scripts.contact
Guest
Posts: n/a
 
      05-12-2007

Damo wrote:
> excellent , cheers that did the trick.
> What does the line,
>
> elem['on'+ev]=lis ;
>


If the tests for addEventListener and attachEvent fails, it sets the
onEventName attribute of element.

For example-if you call
addListener("load",someFunc)

then that line(if it runs) will set:
window.onload=someFunc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adaptive forms Damo Javascript 1 04-28-2007 05:46 AM
forms authentication -- expired forms cookie vs. not provided forms cookie Eric ASP .Net Security 2 01-27-2006 10:09 PM
Junk mail adaptive filter transferability Jowah Firefox 8 12-01-2004 11:54 PM
adaptive viterbi decoder design sunil VHDL 1 02-19-2004 09:02 AM
Adaptive Junk filters Scott McCarthy Firefox 3 10-17-2003 02:14 PM



Advertisments