Is it possible to catch using the jquery to change the input'a value of a given programmatically? If not, how is it possible and is it possible at all (without strong dances with a tambourine)?

The change function catches the change only through the interface, here is an example :

HTML:

<input type="text" id="test" value="xxx"/> <button onclick="$('#test').val('xaxaxa');">Click Me</button>​ 

Js

 $(document).ready(function(){ $('#test').change( function(){alert($(this).val())}); } ); 

    4 answers 4

    Thank you all for pushing the right thoughts. I decided the question this way :

    HTML

     <input type="text" id="test" value="xxx"/> <button id='button'>Click Me</button>​ 

    Js

     var oldVal = $.fn.val; $.fn.oldVal = oldVal; $.fn.val = function(value){ if(!value) return this.oldVal(); else { return this.each(function(){ $(this).oldVal(value); $(this).change(); }); } }; $(document).ready(function(){ $('#test').change(function(){ alert($(this).val()); }); $('#button').click(function(){ $('#test').val('xaxaxa'); }); });​ 

    Waiting for critics.

    • well, or so to do) this is what I love js for) the only thing is that in a princepe it is possible var oldVal = $ .fn.val; $ .fn.oldVal = oldVal; replace with $ .fn.oldVal = $ .fn.val; - MuFF
    • this is definitely a plus, but why? oldVal refers to val, which you have redefined, why oldVal was not redefined? - Specter
    • and also use return $ ("# test"). val.call (this, arguments); - MuFF
    • @MuFF, and why and why (val.call ...)? @Spectre, apparently does not refer, more precisely refers to the function, and we redefined the fn property - Chad
    • one
      even i'm stupid, that's all right with you: var func1 = function () {return 1;} var func2 = func1; func1 = function () {return 2;} func2 (); // 1 - Specter

    This is similar to the wrong architecture. Probably, you have heard many times that values ​​in objects should not be set directly in properties or read from objects directly from properties - for this it is better to always use getters and setters.

    Here is a similar situation. If you assume that a function that you cannot change will be called from so many places that you cannot even imagine from where, then it is best to wrap it in your own.

    So just wrap $('#test').val('xaxaxa'); in its function and through it always change #test .

    • Isn't val a substitute for a setter / getter? :-) I can just imagine where I’ll call from, so I want to catch this moment by writing a single line, and not replacing a couple of thousands :-) To the extreme, this is also an option, but I wouldn’t call this solution beautiful and correct. Your decision is not suitable at all, and moreover, I am not impressed by the arguments. How do you propose to wrap #test? ala $ ("# test"). myVal ("lala")? The idea is that a certain functionality is hung on changing the #test value, and how this change happened is not important. - Chad
    • .val () function from a third-party library that you use in your code. You can not change the code of a third-party library. This is a bad habit. Yes, you can now change and continue to work on the project you'll know it, and js allows it. But do not train yourself to think in this way. Another person who will work on the code after you will not understand what you mean by that. Changing someone else's code is bad. Its functionality needs to be extended only by inheritance or something like that. - MuFF
    • And no one wants to change the jquery code, there was not even a thought :-) It is necessary to change someone else's code, if no one else is going to change it, regardless of you, but it does not suit you, and this is not a “black box”. - Chad

    And just make the trigger event? ...

     <button onclick="$('#test').val('xaxaxa');('#test').trigger('change');">Click Me</button>​ 
    • Already discussed, comrade @Spectre offered this option in one form or another. Why this does not fit you can learn from the comments. - Chad
     $(document).ready(function(){ $('select, input, textarea, button').change(function(){ alert('Что-то изменилось...'); }); }); 
    • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky