Hi,
I just started learning prototype, and I encountered a problem
while trying Event.observe(window,''load'',func);
When I try to run the code below, I get null for formObject. Shouldn''t
the class be created only after the body loads ? It behaves as though,
the function creates an instance of the class Login even before the
window load runs. How can I run this code correctly ?
My aim is to make the Login class listen to the submit event of the
siginForm and retrieve the values in the form.
//======== login.html ===================<html>
<title> Hello world</title>
<script type="text/javascript"
src="prototype.js"></script>
<script type="text/javascript"
src="newClass.js"></script>
<script type="text/javascript">
Event.observe(window, ''load'',function(){
new Login(''signinForm'').register();
}, false);
</script>
<body>
<form name="signinForm" method="POST"
action="">
UserName :<input type="text" name="username"
id="username"/><br/>
Password :<input type="text" name="password"
id="password"/><br/>
<input type="submit" value="Submit form"/>
</form>
</body>
</html>
//=======newClass.js================var Login = Class.create({
initialize: function(formName){
alert(formName);
this.formObject = $(formName);
alert(this.formObject);
this.uName=this.formObject.username;
this.pass=this.formObject.password;
},
loginValidate: function(){
alert("Login action performed. username => "+this.uName.value
+"password => "+this.pass.value);
},
register: function(){
this.formObject.observe(''submit'',this.loginValidate);
}
});
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Andrea Reginato
2008-Mar-10 09:18 UTC
Re: problem with Event.observe(window,''load'',func);
> <script type="text/javascript"> > Event.observe(window, ''load'',function(){ > new Login(''signinForm'').register(); > }, false); > </script>Try to change it with this code. It is the common way to call any javascript function because it just wait the DOM struncture to be loaded. document.observe("dom:loaded", function() { new Login(''signinForm'').register(); }); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---