dgodfather dgodfather - 26 days ago 6
Javascript Question

Javascript/Wordpress - Form selection conditions

Javascript newbie here. I've got the following partially working code:

jQuery(document).ready(function ($) {
$("#frm_form_70_container input[type=submit]").css('visibility', 'hidden'); //hide submit button by default

$("select[name='item_meta[4418]'], select[name='item_meta[4473]'], select[name='item_meta[4474]'], select[name='item_meta[4483]']").change(function () {
var submit = true;

if ($("select[name='item_meta[4418]']:selected").val() == 'Email' || $("select[name='item_meta[4473]']:selected").val() == 'Email' || $("select[name='item_meta[4474]']:selected").val() == 'Email' || $("select[name='item_meta[4483]']:selected").val() == 'Email')

submit = false;

if (submit) {
$("#frm_form_70_container input[type=submit]").css('visibility', 'visible');
} else {
$("#frm_form_70_container input[type=submit]").css('visibility', 'hidden');
}

});
});


I've got a drop down box with a few values. What I want to happen is for the submit button to hide when anything other than the value "Email" is selected.

This probably seems foolish and I apologize, but any help would be appreciated.

Answer Source

Courtesy of mainly @smerny and myself

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

    var $allSelects = $("#frm_form_70_container").find('select'),
        $submitBtn = $("#frm_form_70_container input[type=submit]");

    $allSelects.on('change', function () {
        if (!$allSelects.filter(function() { 
            return this.value === "Email"; }).length) {
            $submitBtn.hide();
        } else {
            $submitBtn.show();
        }

    });
});

Fiddle Demo