HOW TO access a HTML Label tag's text programmatically

On a technical Forum that I frequent, there was a question on how to get the Label tag's text associated with a radio button (or for that matter any other control). The easiest way I found is to use jQuery -
alert($("label[for='radio1']").text()); //radio1 is the ID of a radio button

You would need to throw in a reference to the jQuery library when you run that statement. The better way is to link to the online copy here - http://code.jquery.com/jquery-latest.js

Is the label tag important anyway? I didn't think so until I read this in W3Schools -
The label element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the label element, it toggles the control.

The for attribute of the label tag should be equal to the id attribute of the related element to bind them together.

Did you know an ASP.NET radiobutton control renders as an INPUT tag with type "Radio" and the radio button's text is rendered with a LABEL tag? So clicking on the radiobutton's text would be as good as clicking it.

Also see: The optgroup tag

Comments

Popular posts from this blog

Maven Crash Course - Learn Power Query, Power Pivot & DAX in 15 Minutes

"Data Prep & Exploratory Data Analysis" course by Maven Analytics

Oracle Cloud Infrastructure 2024 Generative AI Professional Course & Certification Exam (1Z0-1127-24)