Until now, I havenβt done much in javascript. I recently started playing with jQuery and stumbled upon a tutorial on how to make a "Change in Place" form.
The resulting code works fine if you hardcode the id of your container div into a function, but I am having problems generalizing it to the identifier passed to the function.
The code starts as follows:
$(document).ready(function() {
setClickable($('#editInPlace'));
});
function setClickable(target)
{
$(target).click
(
function()
{
$('.buttonA').click(function() { saveChanges(this, false); });
$('.buttonB').click(function() { saveChanges(this, true); });
}
)
.mouseover(function() { $(this).addClass("editable"); })
.mouseout(function() { $(this).removeClass("editable"); });
};
function saveChanges(obj, cancel)
{
$(obj).parent().after('<div id="editInPlace">' + t + '</div>').remove();
setClickable($('#editInPlace'));
}
In the part that I came across, there is the last call setClickableinside the function saveChanges. If I hardcode the value $('#editInPlace'), it works fine. However, if I change the function signature to
function saveChanges(target, obj, cancel)
and pass the target from the setClickable method:
$('.buttonB').click(function() { saveChanges(target, this, true); });
call
setClickable(target);
doesn't seem to work, and I can't figure out why.