Solved

Javascript: getAttribute() on a checkbox

Posted on 2008-10-07
5
2,101 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

770 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