views:

30

answers:

1

I'm using jquery edit in place lib and I'm having a problem when trying to use a class selector to select multiple editable items.

I'm calling editInPlace on an array of elements so I don't have to repeat that call for each editable item.

What I can't figure out is how to append the item id to the url. I'm using rails so I need a url like tags/10

Do you know how to dynamically alter the url or params for each editable item? I tried using the callback param but that cancels the ajax request.

html:

<li class="tag" id="tag_1">bright light</li>
<li class="tag" id="tag_2">soft light</li>

jQuery:

$('.tag').editInPlace({
  url:"/tags/" // NEED TO ADD THE ASSET ID TO THE URL example /tags/10
});
+1  A: 

You can use .each(), like this:

$('.tag').each(function() {
  $(this).editInPlace({
    url:"/tags/" + this.id.replace('tag_','')
  });
});

This just takes the ID and removes the tag_ portion via .replace() before appending it.

Nick Craver
Why not just do: `$('.tag').editInPlace({ url:"/tags/" + $(this).id.replace('tag_','');});`??
Shripad K
@Shripad - Two reasons :) `.id` is a DOM property, so `$(this).id` is undefined...and where this happens, likely `document.ready` `this` wouldn't refer to the `.tag` you're looping over, but `document`, so `this` isn't the element you want...whereas inside the `.each()`, it is.
Nick Craver
I am really sorry about typing in `$(this).id` (i just copy/paste the code). It should have been `$(this).attr(id)`. Also, i thought jQuery dealt with having `this` always refer to the parent function rather than the window/document. Anyways, i guess your are probably right about the second part too. Should try it out. Thanks for clarifying.
Shripad K
@Shripad - You're correct that it *usually* refers to the parent function...and it does here as well, the parent function is the `document.ready` handler. The important thing is that just calling `.plugin(option)` doesn't create a closure, you're still *in* that parent function...whereas `.each()` does create a closure, where `this` refers to the element you want :)
Nick Craver
Awesome! That answered my question accurately. It had to do with closures!! Cool. Thanks for the info :)
Shripad K