Search code examples
javascriptjqueryfunctionreturn

get value from function inside function jquery


I'm really stuck with getting a value from this function, my guess is that it is because of the functions inside the functions.

(ps. I know I don't have to use quotes when I want to get a BOOL value)

Please help

$(document).ready(function() {    
  var text = null;

  function textCheck() {
    if ($('.window-' + sc).has('input[type="text"]')) {
      $(this).find('input[type="text"]').each(function() {    
        $(this).on('click', function() {    
          if ($(this).attr('required')) {
            var placeholder = $(this).attr('original');

            if (!$(this).val() == '') {
              $(this).removeClass('is-invalid shake').addClass('is-valid').attr('placeholder', placeholder);
              text = 'true';
              //return text;    
            } else {
              $(this).addClass('is-invalid shake').attr('placeholder', placeholder + " can't be empty");
              text = 'false';
              //return text;    
            };
          };

        });
      });
    };
    return text;
  };

  var texxt = textCheck();
  console.log(texxt);
});

Solution

  • in you textCheck() function ,the first $(this) stands for window
    change this to your element