See From the below javascript code snippet which one is considered to be most appropriate? [on hold]
Scenario 1:
try { elements.personWrapper.getElement('span[name=personClass]').set('innerHTML',''); } catch(TypeError) { elements.personWrapper.getElement('input[name=personClass]').set('innerHTML', ''); }
Scenario 2:
var span_element = elements.personWrapper.getElement('span[name=personClass]'); var input_element = elements.personWrapper.getElement('input[name=personClass]'); if (span_element) { span_element.set('innerHTML', ''); } else if (input_element) { input_element.set('value', ''); }
Further, OP specifically stated to ignore the fact that the code is user defined
Above code snippet achieve the same goal (Please do not consider the readability or naming conventions, its been edited). So, I would like to know which one is better. Anyone please provide your valuable suggestions.
Please note that either of the one get's set while the page get loaded. The span and input elements are created dynamically. So, the goal is to set the innerHTML if it's a span or set the value if it's an input type element.
I want to know which code snippet is more efficient in terms of code execution and less error prone, Thanks!
The code is obviously relating to an HTML document
and can be trivially substituted for document.querySelector()
, .innerHTML
and .value
without in any way affecting the original intent of the OP or the resulting code execution.
An edit was suggested which substituted stadard DOM methods for the user-defined methods and removed reference to ignore the user-defined methods, which was rejected for the reason that the edit changed the original intent of the question; though am unable to determine how the edited code affects the original question whatsoever.
The question should not have been closed in the first place. The original question is clearly on-topic for Code Review SE, even with the user-defined code at the original question. Kindly re-open the question.
.innerHTML
or.value
. \$\endgroup\$