Yeah, but it seems it has its problems, I was looking into this post
yesterday and stumbled some very weird behavior when using
Event.observe with unextended nodes:
http://groups.google.com/group/prototype-core/t/abd86333d12601f2
Nicolas :-\
Please, while you're using Prototype, *use* Prototype :)
var original = $(aryInp[cptInp]);
var btn = new Element("input", { type: "button", className:
original.className, rel: "ajax" }).setValue( $F(original) );
btn.identify();
original.replace(btn);
btn.observe('click', elm.doCall);
By using plain old DOM methods you will surely stumble into
cross-browser issues, plus, wrting the code with Prototype will make
it much more concise and easier to follow :)
> //And we replace our original button
> aryInp[cptInp].parentNode.replaceChild(btn, aryInp[cptInp]);
> //Event.observe(btn.id,'click',elm.doCall); //This is ok, as I'm
> referencing btn's via it's id
> btn.observe('click', elm.doCall); //this fails, with the same error
> as before: element has no properties :: if (element._eventID) return
> element._eventID;
> Event.observe($(btn),'click',elm.doCall); //this fails too, with the
> same error.
>
> You can see it live, on http://dating.webalis.com/?LNG=fr
> The procedure is trigerred when you click the "go!" button on the top
> left login box.
> If js is disabled, it's a standard form, which post to a url.
> With js enabled, I replace the submit button with a simple button
> which launch an ajax request on the same url, but with different
> parameters.
> The return handler is still plain dumb, as it just alert() the xml
> fragment returned by the server.
If you're only trying to intercept the submit of the form in order to
do it via ajax, replacing the submit button is *not* the best way to
do it, as people can still submit the form "normally" by hitting enter
when focusing a form field. Instead, hijack the form's 'submit' event:
$("the_form_id").observe("submit", function(event) {
event.stop(); // this prevents the 'regular' submit
this.request({
onComplete: function(t) { alert(t.responseText) }
});
});
See http://prototypejs.org/api/form/request too :)
> I've tried the Event.observe(elm,...) call with prototype 1.6.0 and
> 1.5.1.1 Both have the same behavior and fail the same way.
> I would have tried the svn version too if I had rails installed at my
> work computer....
You don't need rails to have prototype trunk :)
svn checkout http://dev.rubyonrails.org/svn/rails/spinoffs/prototype/trunk
prototype
Best,
Nicolas