There are 6 (maybe more) fields (div) on the page; I consider each field as an object.

function Field(obj, params){ .... self init=function(){ Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΏΠΎΠ»Π΅: ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ Π² Π½ΡƒΠΆΠ½Ρ‹ΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ‚; присвоСниС полю элСмСнтов ΠΈΡ‚Π΄ } self edit = function(){ ΠžΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅Ρ‚ ΠΌΠΎΠ΄Π°Π» Π΄Π°Π½Π½ΠΎΠΌΡƒ полю для рСдактирования настроСк ΠΈΡ‚Π΄. } .... self.init(); } 

Tell me how to initialize all fields when loading the page?

  field_1=new Field("id поля"); 

How to find all fields and initialize if each field has an id? How to be if fields are classified?

Write in the comments, if it’s not clear, I don’t know how to formulate the question differently.

  • read about how to make plugins. I think will help. - zb '
  • one
    What is the construction of self init? Can var self = this. self.init = - koza4ok
  • @ kaza4ok the way it is, I did not bring the whole structure. - Yoharny Babay

1 answer 1

I don't know what you meant, but I hope that this code will help you figure it out.

 // создадим ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ-Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ var Field = function (obj, params) { var init = function(){ if (obj.className.search('field-active')===-1) { // Ссли ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Π½Π΅ "ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½" console.log(obj.id); // Π²Ρ‹Π²Π΅Π΄Π΅ΠΌ Π² консоль Π΅Π³ΠΎ id obj.className = obj.className + ' field-active'; // "ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ" ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ } } init(); } divs = document.getElementsByTagName('div') // Π½Π°ΠΉΠ΄Π΅ΠΌ всС div for (i in divs) { var node = divs[i]; if (node.id) Field (node); // ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ, Ссли Π΅ΡΡ‚ΡŒ id } 

Try running it on this page. The first time it will print all the id (among the found div ), the second time it will not print anything, because all objects will be "initialized".

PS And what do you mean by initialization?

  • initialization in my case is a call to the init method. Something like that I wanted. I even thought about it, but I wanted a more beautiful solution. - Fucking Babay