Suppose there is some HTML code:

<div id="main"> <div id="sub1">123</div> <div id="sub2">456</div> <div id="sub3">789</div> </div> 

You need to save each sub* block into an array object in order to access these blocks later. For example:

 var i = 0; var arr = {}; $('#main').find("div").each(function() { arr[i] = {'elem':$(this)}; i++; }); 

But how now, for example, to get access to an element, say number 3, if now we have an object, and not an HTML node?

 arr[3].а_дальше_что? 

I want it to be something like this: arr[3].text(); // get block text

  • var arr = []; - Igor
  • To fill the array, I suppose, the map function is more suitable for you. - Vadim Ovchinnikov

2 answers 2

Well, and refer to the field object

 var i = 0; var arr = {}; $('#main').find("div").each(function() { arr[i] = { 'elem': $(this) }; i++; }); console.log(arr[2].elem.text()); console.log(arr[2].elem.html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main"> <div id="sub1">123</div> <div id="sub2">456</div> <div id="sub3"><span>789</span></div> </div> 

  • Thank! As the saying goes: "Everything elementary is easy!". Do not advise any js-editor, and it’s impossible to learn js in a regular notepad? - Trancer
  • @Trancer learn.javascript.ru/editor - Anton Shchyrov

To get HTML from a DOM element, there is such property outerHTML . From a jQuery object, you need to get a DOM element and access this property:

 arr[3].get(0).outerHTML 
  • I know about the outerHTML property, but did not know how to get close to it. All because of the habit of writing in a notebook)) you need to find some kind of JS IDE for these quietly needs. Thank you very much for your answer! - Trancer
  • @Trancer vote for, since your answer helped you. Concerning IDE purely for FrontEnd it is possible, for example, WebStorm. - Vadim Ovchinnikov