Solved

Javascript: getAttribute() on a checkbox

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now