How can I transfer a page element to my object for further manipulations with it inside the same object?

The following is not a working example for additional clarity in the question:

var elem = { foo: $('#a'), bar: function(){ this.foo.text('aabbcc'); } } elem.bar(); 
  • codepen.io/anon/pen/JGONbJ your example works. Perhaps at the moment of assignment in foo element #a is not yet in the DOM? - Invision
  • Probably, yes, I need to look, thank you, somehow forgot about it - Vladislav Siroshtan
  • It really works, probably you need to tie up with night coding: D - Vladislav Siroshtan

1 answer 1

It looks like you want to do something like your own plugin .

 (function($) { $.fn.bar = function() { var foo = function() { $(this).text('New text'); }; return this.each(foo); }; }(jQuery)); $('.el').bar(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="el">Old text</div> <div class="other_el">Other element</div> <div class="el">Old text</div> 

  • This is my second question to which I wanted to know the answer, but did not know how to state it, thank you very much - Vladislav Siroshtan