Angenommen, ich habe ein HTML-Formular. Jeder Eingang / select / Textfeld wird ein entsprechendes haben <label>
mit dem for
Attribut auf die ID des Begleiters es. In diesem Fall weiß ich, dass jeder Eingang nur eine einzige Bezeichnung hat.
Was ist bei einem Eingabeelement in Javascript - beispielsweise über ein Onkeyup-Ereignis - der beste Weg, um das zugehörige Label zu finden?
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 };