I select several HTMLElements and perform actions on them, in this case I assign a new class (CSS). All this, of course, is implemented in jQuery, but you need to do it yourself.

function $() { var elements = []; for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == "string") element = document.getElementById(element); if (typeof element == "array") elements.concat(element); elements.push(element); } elements.addClass = addClass; return elements; } function addClass(classStr) { for (i in this) { var classArray = i.className.split(" "); if (!inArray(classArray)) { classArray.push(classStr); this[i].className = classArray.join(" "); } } } function inArray(arr, value) { var i; for (i=0; i < arr.length; i++) { if (arr[i] === value) { return true; } } return false; } $("string1").addClass("underline"); 

Does not work, because $ ("string1") does not return values. But how then to implement this? So that all the arguments to the $ () function are assigned a class value?

    2 answers 2

    It is necessary to take into account the shortcomings indicated by @metazet (I mean the identification of the selector by its first character: '#' - then ID'shnik goes, '.' - then the name of the class goes). And implement the installation of the class in the string

     $("string1").addClass("underline"); 

    quite simply, the function $("string1") must return an array object that implements the addClass method and the following logic including logic — iterate over all elements of the array object and call addClass from each of them. As a prototype (the parent of the class), you should choose the Array class, since quite often I meet the lines:

     if ($("string1").length == 10) {...} 
    • First, if you give an example of a code, so at least they would adapt it to the real situation) It would be more appropriate if ($ ("string1"). Length> 1) {...} But I don’t see any difference in your answer from mine, the essence is the same. Sincerely. - metazet
    • Thanks for the answer, about the same I did. Unfortunately, I didn’t quite understand about the prototype .. Is it possible to add .prototype to the Array type, but at the same time add it so that it, the prototype, or rather the method, is used only in arrays that return a $ () function? - Spirit
    • function myType () {return [];} myType.prototype.addClass = addClass; myElements = new myType (); Seems to understand what you said. Thanks again for the answer: I got a lot of useful thoughts. - Spirit
    • @metazet, but I need exactly if ($("string1").length == 10) {...} and nothing else, in my opinion, this is a meaningless remark on your part. The essence is partially the same - you did not offer to return an intermediate proxy object, you suggested manually pushing $ .each to each place, which, by the way, is more inhibited than native for JS. - GLAGOLA
    • @GLAGOLA, I meant that why is the length exactly 10? My proposal was only of an advisory nature, because the cases of checking for dimension == 10 are much less frequent than the check of an array at all or not. I think a misunderstanding arose here :) - metazet

    It does not work, because you have forgotten the symbol of a point or a lattice if string1 is a class, then you need

     $(".string1").addClass("underline"); 

    and if string1 is an id, you need this:

     $("#string1").addClass("underline"); 

    Teach materiel;) Oh yes, and for assigning a class to all objects, you can do this (although the top option should work too, but I will not say for sure):

     $(".string1").each(function(){ $(this).addClass("underline"); });