views:

152

answers:

3

I have code that renders the following HTML

<tr>
  <td>Control 1 with onclick="javascript:Test(this);" </td>
  <td>Control 2</td>
  <td><span><div>Control 3</div></span></td>
</tr>

function Test(myThis)
{
  //Here I would like to get references to the siblings of Control 1 (in this ex: Control 2, Control 3)
  //and disable them. Any ideas?

}

I am using this approach of finding siblings as this is in a nested usercontrol with depth = 3 and ASP.NET refers to the controls with a prefix which is awfully hard especially if i have multiple instances of the control on the page.

+5  A: 

if you're using a javascript library such as jQuery it becomes really easy:

$(this).siblings().disable();

http://docs.jquery.com/Traversing/siblings#expr

If you don't have jQuery or a similar library, then you'll have to do it manually:

function disableSiblingsOf(elem)
{
  var nodes = elem.parent.childNodes;
  for(var i=0; i<nodes.length; i++);
     if(nodes[i] != elem)
        nodes[i].disabled = true;
}

That should do the trick.

Ben Scheirman
A: 

Add jQuery and use the following:

$(myThis).nextAll("td").each(function(){
  // Code to "disable" them here, dunno what you mean by that though as they are tds.
});
svinto
A: 

I'll assume you don't have a javscript library available, since I already see jQuery answers posted.

It depends a lot on the html rendered by your controls. However, you can probably just reference this.parent in a loop until this.tagName === 'TR' or this.parent becomes null. Then descend back down.

Joel Coehoorn