How to exclude a sequence of functions?

0 like 0 dislike
16 views
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..

Perpetrators:
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 | 16 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

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
0 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked Nov 11, 2018 by Gremlin92
24,509 questions
45,932 answers
0 comments
345 users