Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check checkboxes in datagrid using Javascript

Status
Not open for further replies.

chetank

Programmer
Joined
May 3, 2006
Messages
3
Location
US
Hi All,

I have a datagrid with a column of check boxes, a column of drop down lists and couple of other databound columns. My requirement is, when i make a selection in a dropdown list control, the respective rows check box should be checked. In my present functionality the page is making auto post backs as soon asi make a selection in drop down list, user should be able to make selection in different drop down lists of the datagrid and then the page should post back. So i am looking for some java script code to implement this functionality. I would greatly appreciate if someone can help me with this.

Thanks,
Varma
 
It is easy to toggle the checked property of the checkbox but you have to know which checkbox to target.

Set an ID tag for each checkbox and use an onchange event in your select box to pass that checkboxes ID to a function check the checkbox.

Code:
function setCheck(which) {
  document.getElementById(which).checked = true;
}

<select name="myselect" onchange="setCheck(chkBox1)">......
[code]

You might however want the box to be able to un-check if they go back to a default value in which case you might want to pass the value of the select box also and conditionally toggle the checked value.

[code]
function setCheck(which, val) {
  if (val == "default") 
    document.getElementById(which).checked = false;
  else
    document.getElementById(which).checked = true;
}

It's hard to think outside the box when I'm trapped in a cubicle.
In an increasingly self-focused society it is important to recognize the unselfish actions of others so they will feel encouraged to continue such actions. Please give acknowledgement to those who aid you whether it is waving to the person who let you out in traffic, tha
 
Thank you very much for the reply.

But my Problem is that i am unable to find the names of the controls in that datagrid. Coz thatz a dynamic grid that gets populated based on some selection on that page. So how do i know whatz the name of the dropdown control on that particular row of the datagrid and how do i match it with that particular row's check box.

Please help me out with a solution to this problem.. Thanks
 
Check the HTML and find out if the name of the checkbox can in any way be associated with the name of the select box so you can match the two.

You can use document.getElementsByTagName("select") to get a collection of all the select boxes on the page, loop through them to get each ones name and use that name to figure out what the associated checkbox name would be.



It's hard to think outside the box when I'm trapped in a cubicle.
 
when dropdown is changed how to identify which row is changed.
 
You will have to show the HTML of the page after it has been rendered and tell us which part is dynamically generated. It's all guessing until I know what the output looks like.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top