Hello!

Please tell me how you can fix this error: Uncaught TypeError: y is not a function

function test($t, y) { y($.trim($($t).val())); } function validateTest(z){ alert(z); } $('.requed').on('change', function() { test('#'+$(this).attr('id'), 'validate'+$(this).attr('data-var')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <label for="signin-login">Логин</label> <input class="full-width requed" id="signin-login" data-var="Test" type="text" placeholder="Логин" name="login"> <span class="cd-error-message"></span> </p> 

As I understand it, an error occurs when trying to call a value as a function. I need this way. I have many fields and based on the value ( 'validate'+значение из data-var ) that came from the field, a certain function will be called.

    2 answers 2

    Depending on the context of the executable function. If everything is in the context of a window , you can write this:

     function test($t, y) { var func = window[y]; func($.trim($($t).val())); } function validateTest(z){ alert(z); } $('.requed').on('change', function() { test('#'+$(this).attr('id'), 'validate'+$(this).attr('data-var')); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <label for="signin-login">Логин</label> <input class="full-width requed" id="signin-login" data-var="Test" type="text" placeholder="Логин" name="login"> <span class="cd-error-message"></span> </p> 

    Otherwise, you need to replace window with the name of the ancestor object containing this function.

    • Thank! What you need :) - Dima
    • @ Dima, accept one of the answers by clicking on the check mark to the left of it. a minus of this answer in the room of functions in the global one, and a possible plus in the room of function selection by name inside test . UPDATE: oh, while I wrote the comment, you already accepted :) - Qwertiy
    • @ Aleksey Shimanskiy And how do you like the variant from Qwertiy? Maybe it is better to use it?) - Dima
    • @ Dima, yes, I like his version. About the lack, by the way, of the version with windows - he wrote. - Alexey Shimansky

     function test($t, y) { y($.trim($t.val())); } var validators = { Test: function validateTest(z) { console.log(z); } } $('.requed').on('change', function() { test($(this), validators[$(this).attr('data-var')]); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <label for="signin-login">Логин</label> <input class="full-width requed" id="signin-login" data-var="Test" type="text" placeholder="Логин" name="login"> <span class="cd-error-message"></span> </p> 

    • Thank you! :) Also a great option! - Dima
    • By the way, in your version, as I understand it, the function is not called by name from the data-var. And what if 5 functions of such will be (and 5 fields with different values ​​in data-var, respectively)? - Dima
    • @ Dima, by name: validators[$(this).attr('data-var')] - the validators object field is taken with the name from the data-var attribute. In the next answer, the same thing is done, but in a different place from the global scop: window[y] . - Qwertiy
    • one
      @ Dima, var validators = { Test: function (z) { ... }, Name: function (z) { ... } } - Qwertiy
    • one
      @ Dima, this is your bug in that id are the same. And in general, there is absolutely no point in looking for the element again when it is already there - improved the script. jsfiddle.net/f425pwq1/6 - Qwertiy