jQuery.grep(array, function(elementOfArray, indexInArray) [, invert])
- as you might guess from the name, the method filters the array elements relying on the result of the function (the original array does not change). JQuery also has a similar .filter()
method which serves to filter the set of items found. var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; console.log($.grep(arr, function(el){ return el % 3 === 0 })); // [3, 6, 9, 12, 15]
.map( callback(index, domElement) )
- the method is useful in that it allows you to do transformations over selected elements or an array without writing loops (for arrays, use jQuery.map()
). var links = $('a').map(function(el){ return this.href; }).get().join("\n"); console.log(links);
jQuery.proxy( function, context )
- it can be useful if you need to pass a callback, but set its own context for it (the value of the variable this
). var myObj = { message: 'hello', alertMessage: function(){ // this myObj, // , alert(this.message); return false; } }; $('a').click($.proxy(myObj.alertMessage, myObj));
.prop( propertyName, value )
- this method appeared in version 1.6 and I have been using it since about the same time. But I decided to mention here because many mistakenly use the .attr()
method to access the properties of elements and modify their values..attr()
works directly with the attribute of the element and in some cases the result is not exactly expected..attr()
can .attr()
its default value (which can be seen in the HTML source of the page). In this case, we need to use the .prop()
method - it will return the current value of the element property. $('input').prop('checked', function(el, oldVal){ return !oldVal; });
.serializeArray()
is a very simple way to serialize form data as an array or string (in this case, use .serialize()
). The method returns an array, which then for example can be sent via ajax to the server. var arr = $('#myForm').serializeArray(); // arr {name: "field-name", value: "field-value"}
Source: https://habr.com/ru/post/146630/
All Articles