i used in this below code in usercontrol

but error happen

because the checkbox value didn't pass

please help how to pass the checkbox value

i try <!--onclick="checkedChanged(this,'checkbox')--> this line

but the checkbox is runat=server

so different value is generated in html view source code...

so doesn't work this code...........

function checkedChanged(PanelId,ChkID)
{
if(ChkID.checked == true)
                    {
                    PanelId.style.backgroundColor="Yellow"; 

                    return true;

                    } 
            if(ChkID.checked == false)
                    {
                    PanelId.style.backgroundColor="YellowGreen";    
                            }
                    return false;

                        }           


<div id="Panel1" style="WIDTH: 64px; HEIGHT: 48px; BACKGROUND-COLOR: yellowgreen" onclick="checkedChanged(this,checkbox) runat="server">
        <img id="imgcolor" style="WIDTH: 32px; HEIGHT: 31px" height="31" src="file:///\\system107\HIS\Images\seat2.jpg"
            width="32" align="middle" runat="server">   
             <input type="checkbox" id="Checkbox1" name="Checkbox1" runat="server">
        <label id="Label1" runat="server"></label>
    </div>

Edited 3 Years Ago by pyTony: fixed formatting

There are several things wrong.

- The div and img tags don't have an onclick attribute.

- None of those tags has a runat attribute. The html tag is the only tag with that attribute.

- The runat attribute works on only Microsoft browsers and servers.

- Most attribute catalogs mark runat as "not currentlysupported".

- the value "checkbox" does not exist at the div tag. You need to call the function from the input tag.

thanks for your reply........

i am used usercontrol to placed many times

that is why i am using runat=server attribute


<div id="Panel1" style="WIDTH: 64px; HEIGHT: 48px; BACKGROUND-COLOR: yellowgreen" onclick="checkedChanged(this,Checkbox1)" runat="server">
<img id="imgcolor" name="img" style="WIDTH: 32px; HEIGHT: 31px" height="31" src="file:///\\system107\HIS\Images\seat2.jpg"
width="32" align="middle" runat="server">
<input type="checkbox"
onclick="javascript:var temp=this;alert(temp);"

id="Checkbox1" name="Checkbox1">
<label id="Label1" runat="server"></label>
</div>


i want to pass temp value to the onclick="checkedChanged(this,temp)"

how to pass the checlbox this value or temp value in that function..

please reply.......

First, please enclose your code in the code pseudocode tags (in square brackets), as I have done below. Otherwise, your code turns into zxnrbl through auto formatting.

<div id="Panel1" style="WIDTH: 64px; HEIGHT: 48px; BACKGROUND-COLOR: yellowgreen" onclick="checkedChanged(this,Checkbox1)" runat="server">

The above tag can't possibly work, for the following reasons:
- The div tag doesn't know how to use an onclick attribute, because a div box isn't a clickable object.
- The value "Checkbox1" is not defined in the scope the div tag can see. It should be in single quotes.
- The runat attribute must be in the html tag itself, not in the individual tags for web page elements.

I suggest totally removing the onclick and runat from this tag.

Now for the next tag:

<img id="imgcolor" name="img" style="WIDTH: 32px;
  HEIGHT: 31px" height="31" 
  src="file:///\\system107\HIS\Images\seat2.jpg"
  width="32" align="middle" runat="server" />

There are numerous reasons why this tag doesn't work:
- Styles are always lowercase. Change the case in your styles attribute, or to make it better, put them in a style in the stylesheet.
- the runat tag is not defined for individual tags, but only for the html tag. Remove it and put it in the html tag.

Now for the next tag:

<input type="checkbox" 
  onclick="javascript:var temp=this;alert(temp);"
  id="Checkbox1" name="Checkbox1" />

It doesn't do anything except tell you where you are! As soon as you leave the Javascript part of the onclick in this tag, the value of the variable declared in it can be forgotten. It is not guaranteed to carry over to other JavaScript calls (some browsers do, some don't). Only global variables and the contents of input elements are retained.

This is really where you need to call your checkedChanged function, instead of in the div. Then, in the Javascript, refer to the background color of the div by using its complete variable description, including its id. You can pass it as a parameter in single quotes.

The last tags:

<label id="Label1" runat="server"></label>
</div>

The runat attribute is not defined here either.

i want to pass temp value to the onclick="checkedChanged(this,temp)"

how to pass the checlbox this value or temp value in that function..

please reply.......

The problem is that you are calling the function from the wrong tag. Call it from the input tag.

I'm not disputing the validity of the rest of your post; but:

The onclick attribute can be given, correctly, on (almost) every visible element.
See http://www.w3schools.com/jsref/jsref_onclick.asp for a full list.

Thank you.

Apparently there is an omission in my favorite reference book that left div out of the list of tags which can use the set of standard events. I tried it, and it works. I annotated the list in my book.

This article has been dead for over six months. Start a new discussion instead.