views:

699

answers:

3

Right now I'm using Jeditable for edit-in-place functionality. Jeditable has some nice options, but (as far as I know), it doesn't allow you to trigger editing except by clicking the element in question.

I.e., suppose my element has id comment. With Jeditable, the only way to start editing is to click comment. What I'd like is to put some small text next to comment (e.g., "Click to edit your comment") that, when clicked, will turn comment into an editable text field (and set up Save and Cancel buttons, etc).

+1  A: 

Check out jQuery's trigger method.

Use code like:

$('#id-for-text').click(function(){
  $('#comment').trigger('click');
});
Corey Hart
its worth noting that trigger is only necessary when you have custom user defined handlers. you can achieve the same effect as the above if you do $("#comment").click()
Darko Z
But I still need a way to make actually clicking "comment" not trigger the editing (i.e., make it possible to trigger edit mode *ONLY* when the user clicks #id-for-text), right?
Horace Loeb
Add a $('#comment').unbind('click')
Corey Hart
Sadly $('#comment').unbind('click') doesn't seem to work -- the field still becomes editable on click. Even when I do $("*").unbind() it's still clickable (though nothing else is, as expected). How could this be possible?
Horace Loeb
+1  A: 

Based on your comments above you could do this:

var editFn = (function(){ return $('#comment').click; })();
$('#id-for-text').click(editFn);
$('#comment').unbind('click');

This tells the label that the click event should be the same as the comment click event, then unbinds the event on the comment.

Not sure if unbind will destroy the reference to the editFn hence have included the closure.

Darko Z
Sadly `$('#comment').unbind('click')` doesn't seem to work -- the field still becomes editable on click. Even when I do `$("*").unbind()` it's still clickable (though nothing else is, as expected). How could this be possible?
Horace Loeb
Hmm the only way i could think that this is still possible is if the author of the plugin used .live() for the event instead of .click(). can you try using ("#comment").die('click') instead of unbind and tell me how that works. Make sure that the selector is the same as passed to the jeditable plugin.
Darko Z
Weird -- it still doesn't work. Try out http://www.appelsiini.net/projects/jeditable/default.html with firebug. Even after doing `$("*").unbind()` and `$("*").die()` you can still click to edit.
Horace Loeb
+3  A: 

Okay, I cracked the case. In this blog post, the author writes

You can now use any custom event for triggering Jeditable.

$(".editable").editable("http://www.example.com/save.php", { 
   event     : "make_editable" 
});

So I did this and then did:

  $("#id-for-text").click(function() {
    $("#comment").trigger('make_editable');
  });
Horace Loeb
good work! glad that solved for you. also useful tidbit of info for the future :)
Darko Z