Im attempting to hide an array option in a visual composer dropdown based on the click of a separate checkbox. I am a horrible noob with JS so Ive pieced together this JS to hide the option. Now it does work, but only after an initial cycle through. AKA I have to check the box once then uncheck it to hide the option. And what I need is for the option to not be there by default when the page is loaded.
Again I dont really actually “KNOW” what im doing I just have a basic understanding. So any suggestions on making it more efficient are certainly welcome. If there is anything else info wise that is needed just let me know. Thanks in advance for your time.
PHP for Dropdown Array
array(
'type' => 'dropdown',
'group' => __('Animations', 'tt_myfile'),
'holder' => 'div',
'heading' => __("Hover", 'tt_myfile'),
'description' => __('Choose your <strong>HOVER</strong> animation', 'tt_myfile'),
'param_name' => "ani_hover",
'value' => array(
'--- Please Choose Your Effect ---' => '',
'Curl Bottom Left' => 'hvr-curl-bottom-left',
),
'save_always' => true,
),
PHP For CheckBox
array(
'group' => __('Style', 'tt_myfile'),
'type' => 'checkbox',
'heading' => __( 'Flat Design', 'tt_myfile' ),
//'admin_enqueue_js' => plugins_url('js/myfile.js', __FILE__), //custom backend JS
'param_name' => 'flat_design',
'class' => 'flat_design',
'value' => array( __( 'Check this box to use flat design.', 'tt_myfile' ) => 'yes' ),
),
HTML Output For The Checkbox
<label class="vc_checkbox-label">
<input id="flat_design-yes" class="wpb_vc_param_value flat_design checkbox" type="checkbox" name="flat_design" value="yes">
Check this box to use flat design.
</label>
JS to Remove/Add The Array Option
jQuery.noConflict();
(function( $ ) {
var myfile_ani_array = {
bind_click: function() {
var flatDesign ='.flat_design';
$(document).on('click', flatDesign, function() {
$(this).change(function () {
var val = false;
$(this).each(function () {
val = val || $(this).is(':checked'); // any checked box will change val to true
});
jQuery('select[name="ani_hover"] option[value="hvr-curl-bottom-left"]').toggle(val); // true=show, false=hide
});
});
}
};
$(function()
{
myfile_ani_array.bind_click();
});
})(jQuery);
based on information given:
Html (note i added a new option to show / hide as you only had the only in there)
Hide the element by default with css (note i am targeting the value of the element, i am using “hide-this” for this example to illustrate)
Ok now the value will be hidden by default and on page load we need to check for this (you can also do this with php). One disadvantage of using jquery is you have to wait for the jquery file to load before using, using native js you could remove any lag to hide the element….note place outside your original code
js fiddle (note you can check how it works by adding checked to your checkbox in the html and clicking on run)
http://jsfiddle.net/utwowzkp/14/
Example inside a function:
updated css:
https://jsfiddle.net/4dvxtgnL/5/
Add to any of param:
then in javascript:
Okay it seems everything I had in place was working exactly as it should. I came to the conclusion if the JS was adding an element style once it was clicked to do display none/block. Then I need some default css to be in place for the initial load. So i added this little nugget to my style sheet and all seems to be working excellent. Sort of a De De De moment on my end haha.
If anyone knows of any browser compatability issues with everything I have here please dont esitate to comment. Thanks to anyone who took the time to give this a thought. And @David thanks for giving this a shot.