Please enable / Bitte aktiviere JavaScript!
Veuillez activer / Por favor activa el Javascript![ ? ]
How to exclude a sequence of functions? - Q&A

How to exclude a sequence of functions?

0 like 0 dislike
How can I bypass the serial functions?

Faced with the problem. In the script there are 2 functions that are executed sequentially.
But if the first function is false(does not find the required values), the second is not working.
Need if first doesn't work, it does not prevent the second run..

resKolichestvoSpalen.forEach(function(item, i, resKolichestvoSpalen) { document.getElementById("kolichestvo-bedroom-value-" + item).checked = true; }); resKolichestvoEtazhey.forEach(function(item, i, resKolichestvoEtazhey) { document.getElementById("kolichestvo-etazhey-value-" + item).checked = true; });
asked by | 28 views

1 Answer

0 like 0 dislike
Apparently there is a bug when document.getElementById() to find the element with the given id – in fact you immediately try to change the property checked.

Two options:
  1. to check whether you found an item and only then to put checked;
  2. wrap every function in try .. catch

The first option is better:
function setCheck(arr, prefix) { arr.forEach( item => { const el = document.getElementById(prefix + item); if (el && el.hasOwnProperty("checked")) el.checked = true; }); } setCheck (resKolichestvoSpalen, "kolichestvo-bedroom-value-"); setCheck (resKolichestvoEtazhey, "kolichestvo-etazhey-value-");
answered by
Welcome to Bug-code Q&A, where you can ask questions and receive answers from other members of the community.

24.8k questions

46.2k answers


12.3k users

24,788 questions
46,220 answers
12,337 users