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);
 }
 }
 });
});
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 18, 2014 at 8:53
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Try if ($('select')) { } and umm if ($('.iCheck').hasOwnProperty('iCheck') { } \$\endgroup\$ Commented Nov 18, 2014 at 9:55

2 Answers 2

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
});
answered Nov 18, 2014 at 11:34
\$\endgroup\$
6
  • \$\begingroup\$ Try firebug it get error \$\endgroup\$ Commented Nov 18, 2014 at 11:37
  • \$\begingroup\$ Wait - what is the iCheck plugin here? \$\endgroup\$ Commented Nov 18, 2014 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\$ Commented Nov 18, 2014 at 11:45
  • \$\begingroup\$ This plugin? \$\endgroup\$ Commented Nov 18, 2014 at 11:46
  • \$\begingroup\$ I dont know why is this question in needed \$\endgroup\$ Commented Nov 18, 2014 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.

answered Nov 19, 2014 at 16:17
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.