0

I have the following code (it's jquery but uses php to add in the dynamic variables):

jQuery(document).ready(function($){ 
    var arr = [ 'select', 'checkbox', 'radio' ];
    var thisForm = 'select#widget-layers-widget-form_builder-<?php echo $this->number; ?>-form_builders-<?php echo esc_attr( $item_guid ); ?>-input_type';

    if ($('select#widget-layers-widget-form_builder-<?php echo $this->number; ?>-form_builders-<?php echo esc_attr( $item_guid ); ?>-input_type option:selected').val() == 'select'){
        $('#select-options-<?php echo esc_attr( $item_guid ); ?>').show();
        }else{
        $('#select-options-<?php echo esc_attr( $item_guid ); ?>').hide();
        }

    $(thisForm).change(function(){
        if ($('select#widget-layers-widget-form_builder-<?php echo $this->number; ?>-form_builders-<?php echo esc_attr( $item_guid ); ?>-input_type option:selected').val() == 'select'){
            $('#select-options-<?php echo esc_attr( $item_guid ); ?>').show();
            }else{
            $('#select-options-<?php echo esc_attr( $item_guid ); ?>').hide();
            }
    });
});

The purpose of this is to check the value of a dropdown menu and show/hide an element based on the selection. Currently, the code is designed to work if the dropdown selection == 'select', however, I want to add an array of results here, essentially, if the dropdown value is equal to 'select', 'checkbox' or 'radio'.

How can I achieve this by writing an array and checking the value against it?

2

1 Answer 1

1

First, don't repeat your code in the init and then in the change (at least, it looks repeated, hard to tell with all the php tbh).

Next, get the values separate from the comparison, this also makes it easier to see what's going on:

var arr = [ 'select', 'checkbox', 'radio' ];
var thisForm = 'select#widget-layers-widget-form_builder-<?php echo $this->number; ?>-form_builders-<?php echo esc_attr( $item_guid ); ?>-input_type';

function showHideSelect() {
    var val = $(thisFrom + ' option:selected').val();
    var select = $('#select-options-<?php echo esc_attr( $item_guid ); ?>')

    if (val == 'select'){
        select.show();
    } else {
        select.hide();
    }
}

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

    $(thisForm).change(function(){
        showHideSelect();
    });
});

Now the part in question is simply if (val == 'select'), which you can change to:

if (arr.indexOf(val) >= 0)
    select.show();
else
    select.hide();
Sign up to request clarification or add additional context in comments.

2 Comments

This is a great answer, thanks. Just out of curiosity, why shouldn't you repeat code in the init and then in the change?
In summary: DRY - Don't Repeat Yourself. If you have the same code twice, there's twice as much chance of there being a mistake. If you fix a mistake in one of them or make a change to the action, but forget (or don't know about the other places) then you introduce bugs/don't fix bugs. It also makes it clear that they are supposed to do the same thing - perhaps on click you want a transition effect, but not on load - by having a single method, it's obvious they should do the same thing, whereas the code in the question as-is it would not be clear if it's a bug or intentional.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.