Suppose I have a number of red colored links, and want to add a click event to them such that when it fires, it sends HREF attribute of link to server (as an AJAX request), and on success, the link color will be changed to green. I want something like:
$$('a.red').addEvent(
"click",
function () {
new Request.JSON({
url: 'script.php',
onSuccess: function(){
[the link wich is clicked].setStyle('color', 'green');
return false;
}
}).get("url="+[url of the link]);
}
);
Sorry if silly question. please edit title to some sensible one.
The problem here is that
thisinside the event handler is not the same asthisinside the Request Class. So What usually is done is to change the reference tothisin a new variable, for examplevar self = this.After that you can use
self.setStyle('color', 'green');By the way, a suggestion, the best would be to have also a class for the green color and switch them by the time of the click.
So here is a code suggestion would be:
and using in the CSS something like:
Example