Having trouble with jQuery variable scope in an image upload plugin
-
So I am building an image upload metabox that allows you to dynamically add image upload fields. When the code is static I can handcode the id of the textfield that houses the image url meta. Dynamically I need to use a variable that will be populated with that id based on which upload button you push.
Everything works great so far in that I’m getting the correct ids and can alert them as a test. The problem is that the upload_id variable is not crossing into window.send_to_editor bit. I am guessing this may have something to do with scope and that part being part of some external wordpress code but really I have no idea.
All I need is to get that variable to read across functions and this should work perfectly. I appreciate your help.
<script> jQuery(document).ready(function() { var upload_id = ''; jQuery('.uploadbutton').click(function() { upload_id = $(this).prev().attr("id"); window.send_to_editor = function(html) { imgurl = jQuery('img',html).attr('src'); jQuery(upload_id).val(imgurl); tb_remove(); } tb_show('', 'media-upload.php?post_id=1&type=image&TB_iframe=true'); return false; }); }); </script>
- The topic ‘Having trouble with jQuery variable scope in an image upload plugin’ is closed to new replies.