Javascript: getAttribute() on a checkbox

Posted on 2008-10-07
Last Modified: 2013-11-07
I have a page that has checkboxes on which I defined attributes (ex.: fileType) depending on which file each checkbox is supposed to open. This works well for me on the server side but I can't get access to those attributes in Javascript. Can some one help?

The code snippet shows how I define them.
//on aspx.cs file


//In javascript

var m = document.getElementById("CheckBox1").getAttribute("fileType"); //This returns null.

Open in new window

Question by:karakav
LVL 20

Expert Comment

ID: 22657072

R u writing this in Pageload event or in


Or Try this

var m = document.getElementById("CheckBox1")
var FilType = m.getAttribute("fileType");
LVL 16

Accepted Solution

ororiole earned 500 total points
ID: 22657099
Those are handy .net server side only attributes. They don't render to the browser. javascript can only read things on the rendered page so you can't access them.

Here is a possible workaround. For each checkbox also add a hidden field. Add your attributes to it, filetype="pdf", path="../files/pdf", etc, separated by commas. Then

var m = document.getElementById("CheckBox1HiddenField").value;

var a;

a = m.Split(',');

//Now each array element is a string that represents your attributes

a[0] is filetype="pdf"

a[1] is path="../files/pdf"


Open in new window

LVL 13

Expert Comment

ID: 22657823
here is what i have done:

custom checkbox:


public class CheckBoxEx : System.Web.UI.WebControls.CheckBox


    protected override void Render(HtmlTextWriter writer)


        if (!string.IsNullOrEmpty(this.FileType))

            writer.AddAttribute("fileType", this.FileType);



    private string _FileType = "pdf";

    public string FileType


        get { return _FileType; }

        set { _FileType = value; }



.aspx page


function clickMe(id)


    //In javascript

    var ob = document.getElementById(id);

    var m = ob.getAttribute("fileType"); //This returns null.



codebehind file


CheckBoxEx CheckBox1 = new CheckBoxEx();  

    protected void Page_Load(object sender, EventArgs e)



        if (!this.form1.Controls.Contains(this.CheckBox1))


            this.CheckBox1.Text = "Test";




        //on aspx.cs file


        CheckBox1.Attributes.Add("onclick", "javascript:clickMe('" + this.CheckBox1.ClientID + "')");


Open in new window


Author Comment

ID: 22657914
Sorry SameerJagdale,

I have been using user and custom web controls and I had many problems with them that I don't consider using them anymore soon. Thanks for your help anyway.

Author Closing Comment

ID: 31503723
Thanks. I don't like the idea of passing attributes to a hidden field but I do consider is the less time consuming solution.

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 31
Visual Studio Publish Web Configuration does not contain Release 5 16
Angular JS Route 3 45
Enter key on html form page 11 44
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now