Good day to you! The question is of course stupid, but to my regret, I can not clearly formulate my question and ask for help from search engines.

For example, on the page, the n-th number of all sorts of different tags that we will fill with the text through the value and innerHTML attributes.

Question:

How to learn programmatically , through which attribute the tag is filled, through value or innerHTML ?

And immediately question number 2: In PHP, there is such an interesting thing as a variable, an example:

<?php $var1 = 'variable!111'; $vb = 'var1'; echo $$vb;//выведет variable!111 ?> 

Is it possible to do something similar in JS, for example like this:

 $prpt_of_el = 'innerHTML'; $var eec = document.getElementById('someEI'); alert(eec.$$prpt_of_el);//хотелось бы динамически обратиться к свойству элемента, тоесть $$prpt_of_el может принимать значение либо innerHTML либо value, в зависимости от значения $prpt_of_el 

I really hope that I put it clearly enough so that you can give me a full answer.

    2 answers 2

    If you understand the problem correctly

     // допустим есть элемент var el = document.getElementById("someId"); // допустим переменная whatToChange строка // которая может быть строкой 'value' или 'innerHTML' // достаточно просто обратится так: var whatToChange = 'innerHTML'; el[whatTochange] = 'Some new innerHTML'; whatToChange = 'value'; el[whatToChange] = 'Some new value'; 

    And it is quite simple to learn programmatically - it is necessary to check an element with a regular expression whether it is filled in via value. Frankly, I didn’t understand how necessary it is in all this, maybe you misunderstand something and want to do something wrong, describe the task you are trying to solve in this way

     // тогда примерно так function fillById(id, data) { var el = document.getElementById(id); if(el !== null) { var t = el.tagName.toLowerCase(); if( t == "textarea" || t == "input" || t == "select") { el.value = data; } else { el.innerHTML = data; } } else throw new Error("Element with id " + id + " not found in the DOM"); } 
    • The task is as follows: several arguments are passed to the function, one of which contains the id of the element where the result of the request will be written, and the second where the status of the request will be written. The element can be filled through innerHTML or value, the condition just needs to know whether to use innerHTMl or value to fill these elements with text. - Andrey Arshinov

    Well, as an option to create an array of tags with a filling by value (of which there are some), and check if the current tag is in the array, then do it by value, otherwise by innerHTML.