views:

38

answers:

1

Hi

I'm attempting to hack a wordpress plugin to gain some extra functionality.

As it stands i've adjusted it so I can add a new textarea with it's own upload box.

I now need a way to get the ID of the new textarea, so I can pass the uploaded file text back to the correct textarea.

this is my code:

JQUERY:

<script type="text/jquery">
jQuery(document).ready(function() {

jQuery('#upload_image_button').live("click", function() {
 var tblID = jQuery(this).closest("textarea").attr("id");
 alert(tblID); // returns undefined.


 formfield = jQuery('#upload_image').attr('name');
 tb_show('','media-upload.php?type=image&TB_iframe=true');
 return false;
});
</script>

as it stands, this doesn't work. I get "undefined", rather than the ID of the text area as I hoped.

the markup for each DIV that gets added is like this, where * indicates a unique number:

<div id="my*Div>
<textarea class="dfrads_textarea" id="dfrads_textarea_*" name="ad_*">
  <!-- my content -->
</textarea>
Upload:
<label for="upload_image"><input type="text" value="" name="upload_image_*" size="36" id="upload_image_*"><input type="button" value="Upload Image" id="upload_image_button_*"><br>Enter an URL or upload an image for the banner.</label>
</div>

so if I add 2 extra DIVs, the * would be "2" in this example. (The second DIV).

How can I get the value of the ID of the textarea that is "paired" with the submit button.

thanks ! I have tried .prev and .closest, but presumably am using it wrongly.

A: 

Since it's not inside the <textarea> you need to traverse a bit differently, replace this:

jQuery(this).closest("textarea").attr("id");

With this:

jQuery(this).closest("label").prev("textarea").attr("id");

This goes up to the <label> then back to the <textarea> previous sibling...your selector isn't quite right on the .live() call, at least not for the markup, but if you're getting an alert at all I'll assume it's just not quite as posted and is working correctly.

Nick Craver
thanks, that makes sense but still returns "undefined". The original code didnt have `.live` call - it was a .click(), but with just `.click`, the jquery didn't pick up the newly generated html (i think)
calum
scratch that, works fine, was me being a muppet. What's wrong with the .live() call if you don't mind enlightening me. thanks again
calum
@calum - The `.live()` call is referencing the ID `upload_image_button` which *should* be changing, so no new buttons should match it...either you're getting duplicate IDs or something else is going on. I'd be safe and remove the ID, just change the button to having a class and `.live()` bind to that.
Nick Craver
makes sense, will have a crack. cheers
calum