How To add an attribute to an HTML element and change its value at run-time?
9 Dec 2019 / 1 minute to read
You can add an attribute to an HTML element using the Add method of the Attributes property, as shown in the following code snippet.
// textBox is an INPUT element of type 'text' in the HTML document
if(this.textBox.Attributes.Contains("style") == false)
this.textBox.Attributes.Add("style");
' textBox is an INPUT element of type 'text' in the HTML document
If Me.textBox.Attributes.Contains("style") = False Then
Me.textBox.Attributes.Add("style")
End If
You can change the value of an element’s attribute at run time by using the Value property of that particular attribute, as shown in the below code snippet.
// textBox is an INPUT element of type 'text' in the HTML document
this.textBox.Attributes["style"].Value = "background-color:red";
' textBox is an INPUT element of type 'text' in the HTML document
Private Me.textBox.Attributes("style").Value = "background-color:red"
Was this page helpful?
Yes
No
Thank you for your feedback!
Thank you for your feedback and comments. We will rectify this as soon as possible!
An unknown error has occurred. Please try again.
Help us improve this page