4

Given

$(".foo").editable("script.php")

How can I pass the ID of .foo's parent as a parameter? I've tried

$(".foo").editable("script.php", { 

     submitdata: {

          parent_id: $(this).parent().attr('id')

                 }
     });

and every variant of that I can think of, but it just seems like the $(this) object is not working in this context.

Matthew
  • 7,605
  • 7
  • 39
  • 39

3 Answers3

15

I had the same problem here, found the following solution:

$('.jeditable').each(function() {
   var $this = $(this);
   $this.editable('submit.jsp', {
      submitdata  : { parent_id : $this.parent().attr('id') }
   });
});
Darren
  • 68,902
  • 24
  • 138
  • 144
user168162
  • 151
  • 1
  • 3
  • 7
    This works but it's probably worth explaining what's going on... Referring to `$(this)` inside `.editable()` won't work, instead we loop through the desired elements one at a time using jQuery's `.each()`. Inside is the normal call to `.editable`, but with the jQuery object copied into a variable which jEditable *can* read - and which you can then run all the usual jQuery commands on. The use of '$' rather than 'var this' is an aide-memoir to distinguish normal variables & jQuery objects - http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign – William Turrell Nov 22 '12 at 16:54
0

You can do an Ajax call so you can pass everything (if you are looking for the future, maybe you are going to add few features..).

Example:

$('.edit').editable(
    function(value, settings) {

        // prepare the data, add all parameters you want

        var data = "param1=value1" +
            "&param2=value2";

        var ret = "";

        // make the ajax call to do the work (not async)
        $.ajax({
            type: "POST",
            url: "page.php",
            data: data,
            async: false,
            success: function(msg) {

                if (msg.length != 0) {

                    // set the value to write in the element
                    ret = msg;

                }

            }

        });

        // return the element to save into the element
        return ret;

    }, {

        // set the jeditable setting
        cssclass: 'classname'
    }

);

I think that this method is the more flexible for handle every cases.

Justin
  • 84,773
  • 49
  • 224
  • 367
Darion Badlydone
  • 897
  • 14
  • 37
-1

I would just do this:

$(".foo").editable("script.php", { submitdata: { parent_id: $(".foo").parent().attr('id') } });

You also have a typo in your question, you have $(.foo).editable() instead of $(".foo").editable(). If that is also in your actual code, that may be the source of the problem.

Julian
  • 2,021
  • 16
  • 21
  • That was just a typo in the question. Thanks for the response, that does get the ID of the parent, and would be fine if it was $("#foo") instead of $(".foo").. but in this case, it's a class, and there's multiple $(".foo")s... so i need it to reference itself, and not the whole class. – Matthew Apr 09 '09 at 15:12