And now you have added a class to the metabox you just created!Adding your custom class to the each of the metaboxes, sets up the jQuery to do its thing. Here’s the simplified version:
function metabox_script() {
wp_enqueue_script( 'jquery' );
$script = '
<script type="text/javascript">
jQuery(document).ready(function($) {
$("#metabox_id_name").hide();
$("input[name$=\'input_id_name\']").click(function() {
var type = $(this).val();
$("div.added_class").hide();
$("#metabox_id_name").show();
});
});
</script>';
return print $script;
} // End of metabox_script()
First, the script hides the metabox with the matching id. Then, it listens to the inputs with the matching input_id_name. If the value of that input matches the added_class, then it shows that metabox. Simple enough.
In my plugin’s case, I’m creating seven metaboxes and showing and hiding just one based on the radio button selection. Instead of writing seven show/hide statements, I used this code, which is the jQuery equivalent to a foreach statement:
function metabox_script() {
wp_enqueue_script( 'jquery' );
$script = '
<script type="text/javascript">
jQuery(document).ready(function($) {
$.each(["app", "client", "gallery", "position", "site", "theme", "video"], function() {
$( "#slushman_portfolio_" + this + "_fields" ).hide();
});
$("input[name$=\'slushman_portfolio_type\']").click(function() {
var type = $(this).val();
$("div.slushman_portfolio_type_fields").hide();
$("#slushman_portfolio_" + type + "_fields").show();
});
});
</script>';
return print $script;
} // End of metabox_script()
And there you have it. Now, your jQuery will show/hide a metabox of your choosing.





