ASP.NET Get a CheckBox value from javascript

Recently I needed to access the value of a CheckBox from javascript.

Because CheckBox doesn’t have a Value property I tried the following:
myCheckBox.Attributed.Add("value", "test");
And tried to access it from javacstipt:

I was surprised to find out that the message in the alert was “on” and not “test” as I expected so I looked at the source of the page and this is what I saw:

As you can see the value I set on the server side code doesn’t appear in the result html code.
After a little more research I found out there is a property for the CheckBox that is called “InputAttributes” – this property adds the attributes to the CheckBox input.
This is the correct code:

myCheckBox.InputAttributes.Add("value", "test");
That’s all I needed.
Happy coding.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s