ben
ben

Reputation: 29807

Question about using a jQuery plugin over a series of matched elements

I am using jQuery In Place Editor to make allow in place editing of a few text elements in my page.

$(".entry_name").editInPlace({
    url: "/entries/change_name",
    params: "entry_id=XXX"
});

The params option allows me to send some parameters along with the post request. I need to specify a different entry_id for each element that matches the .entry_name selector. An example of a matched element is:

<span id="entry_name_24" class="entry_name" data-entry_id="24">example</span>

So in this example, the params option should be entry_id=24

How can I do this?

Upvotes: 1

Views: 62

Answers (3)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

You need to use

$(".entry_name").each(function(){
    var currentID = $(this).data('entry_id');

    $(this).editInPlace({
        url: "/entries/change_name",
        params: "entry_id=" + currentID 
    });
});

But keep in mind that the plugin (according to the Usage section) already passes the id of the element when submitting, but with another name element_id

Once the in-place editor form is submitted, it sends a POST request to the URL that is specified in the editor’s parameters along with three form fields:

  • original_html; the original text in the in-place editor container
  • update_value; the new value of the text from the in-place editor
  • element_id; the id attribute of the in-place editor

edit : sorry, missed that you are getting the entry_id and not the actual id (although you could extract it from the actual id)

Upvotes: 0

Vivek
Vivek

Reputation: 11028

this will do the trick-

$(".entry_name").editInPlace({
    url: "/entries/change_name",
    params: "entry_id"+this.data('entry_id')
});

Upvotes: 0

Paul
Paul

Reputation: 141907

You need to call editInPlace on each element separately:

$(".entry_name").each(function(){
    $(this).editInPlace({
        url: "/entries/change_name",
        params: "entry_id="+$(this).data('entry_id');
    });
});

Upvotes: 2

Related Questions