2
\$\begingroup\$

I think there is more that is needed here. Can someone please comment on it?

What I need is to add some common functions to check if an element exists.

// Common functions
$(document).ready(function() {

    /*
    Function for creating different styling checkbox, radio input, and select
    */

    // Create pretty checkboxes and inputs

    if ($('.iCheck').length){
        $('.iCheck').iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue',
        increaseArea: '20%' // optional
    });
    }

    // Create pretty selects and multiselect
        if(!$("html").hasClass("ie8")){ 
            if ($('select').length){
                $('select').attr('data-width', '100%').selectpicker();
            }

        };

     /*
      Preloader 
     */
    var targetPreloader = $('[data-overlay-text]');
    targetPreloader.click(function() {
        var text = $(this).attr("data-overlay-text");
        $('.preloader-text').text(text);
        $('.preloader-window').show().fadeIn();
    });

    /*
      Fix for showing SVG
     */
    svgeezy.init('nothing', 'png');


        /*
    Form validation using Jquery validate
    */      
        $('form').validate({
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
            if(element.parent('.input-group').length) {
                error.insertAfter(element.parent());
            } else {
                error.insertAfter(element);
            }
        }
    });

});
\$\endgroup\$
  • 1
    \$\begingroup\$ Try if ($('select')) { } and umm if ($('.iCheck').hasOwnProperty('iCheck') { } \$\endgroup\$ – iLikePrograms Nov 18 '14 at 9:55
3
\$\begingroup\$

You shouldn't even need to do a check for element exists before calling a jQuery method. All internal methods, and any plugin that follows good practices, just won't do anything at all if there are no elements in the set. So you could just simplify that part of the code down to (without the if):

$('.iCheck').iCheck({
    checkboxClass: 'icheckbox_minimal-blue',
    radioClass: 'iradio_minimal-blue',
    increaseArea: '20%' // optional
});
\$\endgroup\$
  • \$\begingroup\$ Try firebug it get error \$\endgroup\$ – Miomir Dancevic Nov 18 '14 at 11:37
  • \$\begingroup\$ Wait - what is the iCheck plugin here? \$\endgroup\$ – Scimonster Nov 18 '14 at 11:42
  • \$\begingroup\$ It changes the styling of checkbox and radio buttons, the problem is what if i dont have inouts on page \$\endgroup\$ – Miomir Dancevic Nov 18 '14 at 11:45
  • \$\begingroup\$ This plugin? \$\endgroup\$ – Scimonster Nov 18 '14 at 11:46
  • \$\begingroup\$ I dont know why is this question in needed \$\endgroup\$ – Miomir Dancevic Nov 18 '14 at 11:48
1
\$\begingroup\$

I had a similar question myself, and after reading this question from SO, I have reached the conclusion that the check you have used is probably just about as good as it gets, though that question does have other suggestions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.