0

I have a custom post type with a metabox text input area that I want to have a shortcode inserted into from a thickbox when a link is clicked on from the modal window. Using the code below right now it just closes the modal window when the insert button is clicked. Please tell me where I am going wrong.

I have the thick box opening fine.

The page that is rendered has the link as this:

echo '<td><a href="#" style="display:block" class="button insertdownload" id="download-'.$d->id.'">Insert</a></td>';

I have the jQuery call as:

jQuery('.insertdownload').click(function(){

var did = jQuery(this).attr('id');

did=did.replace('download-', '');

if (jQuery('#format').val()>0) {

jQuery('#my_new_field').val('[download id="' + did + '" format="' + 

jQuery('#format').val() + '"]');

} else {

jQuery('#my_new_field').val('[download id="' + did + '"]');

}

window.parent.tb_remove();
});

I also have my metabox text field as:

<input type="text" size="25" name="myplugin_new_field" id="my_new_field">

This was the original code but I didn't want it to go to the editor window, I want it to go to my input field.

jQuery('.insertdownload').click(function(){
var win = window.dialogArguments || opener || parent || top;
var did = jQuery(this).attr('id');
did=did.replace('download-', '');
if (jQuery('#format').val()>0) win.send_to_editor('[download id="' + did + '" format="' + jQuery('#format').val() + '"]');
else win.send_to_editor('[download id="' + did + '"]');
});

2 Answers 2

0

What your code in;

jQuery(document).ready(function(){

// your code here...

});

...and in fact your code should look like this;

jQuery(document).ready(function($){

    $('.insertdownload').click(function(){

    var did = 'this is only a test';

    did=did.replace('download-', '');

    if ($('#format').val('1')) {

    $('#my_new_field').val('[download id="' + did + '" format="' + 

    $('#format').val() + '"]');

    } else {

    $('#my_new_field').val('[download id="' + did + '"]');

    }

    window.parent.tb_remove(); 

    });

});
​

Check out the Fiddle here... LINKAGE

1
  • Thanks, but this doesn't work for me. I tried your code and now the window no longer closes and the value is not inserted. I also needed the check for the #format value to see if it is larger than zero (that is a select dropdown that I want to do the first part of the if/else statement if the value is larger than 0.
    – Ken
    Commented Apr 13, 2012 at 15:40
0

I got it, since it was an iframe I guess I was needing to target the parent window.

    jQuery('.insertdownload').click(function(){
    var did = jQuery(this).attr('id');
    did=did.replace('download-', '');
    if (jQuery('#format').val()>0) {
    parent.jQuery('#my_new_field').val('[download id="' + did + '" format="' + jQuery('#format').val() + '"]');
    } else {
    parent.jQuery('#my_new_field').val('[download id="' + did + '"]');
    }
    window.parent.tb_remove(); 
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.