i would like to “rebuild” a short snippet:
jQuery.fn.getCheckboxVal = function(){
var vals = [];
var i = 0;
this.each(function(){
vals[i++] = jQuery(this).val();
});
return vals;
}
to such like this:
function getvar() {
var vals = [];
var i = 0;
this.each(function(){
vals[i++] = $(this).val();
});
return vals;
}
can anybody help to get the “second” works to? (Link to the Full Snippet above: http://trentrichardson.com/2009/01/08/getting-a-list-of-checkbox-values-with-jquery/)
Thanks 🙂
In the first code, the
thiskeyword points to the jQuery object that you are calling the method on, so you would have to replace that with a jQuery object in the second code:Usage example:
You can also use the
mapmethod to make it simpler: