Question

I need to do a simple boolean condition on a form field. Basically I just need to know if field #xxx is hidden or not.

I think this is the right approach but I'm not sure because I not getting my desired results.

if($("#input_<?php echo $form["id"]; ?>_27[type='hidden']").val() == '') {
// the field has no value so let's break out of this
break;
}

I'm adding a hook to gravity forms in Wordpress. I need this hook to work on all forms because this form may be duplicated, however if a new form is created that contains the same field ID but is NOT hidden I don't want to continue the jQuery function.

EDIT: I added the GF id being passed by the hook for more accuracy. I realized after I posted this that if I duplicated that form the field id would change.

SOLVED: Using Shivam's answer below I got it. For anyone interested entire hook is here:

<?php
add_filter('gform_pre_render', 'replace_cn');
function replace_cn($form){
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($){
            var cname =  $("#input_<?php echo $form["id"]; ?>_27[type='hidden']").val();
            if (!$("#input_<?php echo $form["id"]; ?>_27").is(":hidden")) {
                return;
            }
            var placeholder = '[XX]';
            jQuery("*").each(function () {
                if (jQuery(this).children().length == 0) {
                    jQuery(this).text($(this).text().replace(placeholder,cname));
                }
            });
            jQuery("label").each(function () {
jQuery(this).html($(this).html().replace(placeholder,cname));
            });         
        });
    </script>
<?php
return $form;
}
?>
Was it helpful?

Solution

$("#input_3_27").is(":hidden")

Also more secure way to achieve same effect is:

('#yourID').attr('type') == 'hidden'; // this will also return a boolean

EDIT: By secure I mean, the .is(:hidden) will still work if you style the element with display:none.

OTHER TIPS

Your selector is incorrect see this jsfiddle

$("input[type='hidden']").val() 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top