Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2125
  • Last Modified:

Javascript: getAttribute() on a checkbox

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
karakav
Asked:
karakav
1 Solution
 
Obadiah ChristopherCommented:
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
 
ororioleCommented:
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
 
SameerJagdaleCommented:
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
 
karakavAuthor Commented:
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
 
karakavAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now