Observe field using prototype Rails 3

Does anyone knows how to do an observe field using Prototype in Rails
3. An example would be useful as I am very weak programming in Ajax.

Don Mapp wrote in post #956253:

Does anyone knows how to do an observe field using Prototype in Rails
3. An example would be useful as I am very weak programming in Ajax.

Prototype

document.observe(“dom:loaded”, function() {
$(‘foo’).observe(‘click’, function(event) {
this.setStyle({backgroundColor: ‘blue’});
});
});

jQuery

$(document).ready(function() {
$(’#foo’).click(function() {
$(this).css(“backgroundColor”, “blue”);
});
});

Notice that this begins observing the “click” event on the element with
id=“foo” attribute. Also, notice that it waits for the DOM to fully load
before attaching the event listener.

If you want to observe something other than “click” the just replace
that with the event you want to observe (e.g. “change”).

jQuery

$(document).ready(function() {
$(’#foo’).click(function() {
$(this).css(“backgroundColor”, “blue”);
});
});

Sorry but this is not equivalent to observe

this is

$(document).ready(function() {
$(’#foo’).live(“click”,function() {
$(this).css(“backgroundColor”, “blue”);
});
});

and it can be shorter

$($(’#foo’).live(“click”,function() {
$(this).css(“backgroundColor”, “blue”);
}));

Thanks GUys I will try that and get back to you sometime.

to explain my previous comment, if you do

$(document).ready(function() {

$(’#foo’).click(function() {
$(this).css(“backgroundColor”, “blue”);
});
});

and you change the node holding the “foo” element, since you attached
the
click event on document ready, the event will not work again after the
change, so with the live event you make the browser keep looking for
“foo”
element like the observe event from prototype, so if you update somehow
the
“foo” element the click event will be reattached

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs