Solved

Javascript: getAttribute() on a checkbox

Posted on 2008-10-07
5
2,098 Views
Last Modified: 2013-11-07
Hi,
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

CheckBox1.Attributes.Add("fileType","pdf");
 

//In javascript

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

Open in new window

0
Comment
Question by:karakav
5 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 22657072
CheckBox1.Attributes.Add("fileType","pdf");

R u writing this in Pageload event or in

if(!isPostBack)
{
CheckBox1.Attributes.Add("fileType","pdf");
}

Or Try this

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

Accepted Solution

by:
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"

etc

Open in new window

0
 
LVL 13

Expert Comment

by:SameerJagdale
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);

        base.Render(writer);

    }
 

    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.

    alert(m);

}
 

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";

             

            this.form1.Controls.Add(this.CheckBox1);

        }
 

        //on aspx.cs file

        //CheckBox1.Attributes.AddAttributes(

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

    }

Open in new window

0
 
LVL 4

Author Comment

by:karakav
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.
0
 
LVL 4

Author Closing Comment

by:karakav
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.
0

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