Diciamo che ho un modulo html. Ogni input / select / textarea avrà una corrispondenza <label>
con l' for
attributo impostato sull'id del suo compagno. In questo caso, so che ogni ingresso avrà solo una singola etichetta.
Dato un elemento di input in javascript, ad esempio tramite un evento onkeyup, qual è il modo migliore per trovare l'etichetta associata?
function getInputLabel(thisElement) { var theAssociatedLabel,elementID; elementID = thisElement.id; theAssociatedLabel = thisElement.parentNode.querySelector("label[for='" + elementID + "']"); console.log('theAssociatedLabel.htmlFor: ' + theAssociatedLabel.htmlFor); theAssociatedLabel.style.backgroundColor = "green";//Set the label background color to green };