Understanding the use of non-operator in: selected

I just stumbled upon this FIDDLE online. JS is as follows:

$(function() { $('#add').click(function() { return !$('#select1 option:selected').appendTo('#select2'); }); $('#remove').click(function() { return !$('#select2 option:selected').appendTo('#select1'); }); }); 

HTML ::

 <SELECT id="select1" name="select1" size="3"><OPTION value="1">Test 1</option><OPTION value="2">Test 2</option></select> <SELECT id="select2" name="select2"></select> <br><input type="button" id="add" value="Add >>"><input type="button" id="remove" value="<< Remove"> 

This is basically a piece of JS replacing the selected values. But I do not understand how to use the operator ! (not an operator).

Now I understand that the operator not inverts the result, but in the code above, what does it really do? I don’t understand what the operator does not in the aforementioned violin, and what effect does this have on the final result? Can someone explain?

+5
source share
1 answer

This is a fancy way:

 $('#add').click(function() { $('#select1 option:selected').appendTo('#select2'); return false; }); 

return false prevents the default behavior of the button by canceling the event.

Since the object will always be true !object will be false and $(selector) returns the object

+5
source

Source: https://habr.com/ru/post/1232820/


All Articles