Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my code .
SQL
chbxSelect = new CheckBox();
                chbxSelect.ID = "chbx" + i.ToString(); 
                chbxSelect.Checked = true;
  chbxSelect.Attributes["onclick"] = "AttachmentsSelectionChange(this);";
                chbxSelect.Text = fileName.ToString();
                chbxSelect.TextAlign = TextAlign.Left;


C#
function AttachmentsSelectionChange(cbh) {
                      var UnCheckedfilename = cbh.nextSibling.innerHTML;

}

Here the value of var UnCheckedfilename is correct if i set TextAlign.Right . but i have to get the same value when i set TextAlign.Left. how to get it ?, help me out .
Posted
Comments
ZurdoDev 24-Apr-13 8:05am    
.previousSibling would be going the other way but this seems like a bad way of doing things. What exactly are you trying to do?

If you set TextAlign to Left, then you can access the value by setting
JavaScript
var UnCheckedfilename = cbh.previousSibling.innerHTML;


because "the previousSibling property returns the element that immediately precedes the current element in the childNodes collection of the current element's parent."

where as "the nextSibling property returns the element that immediately follows the current element in the childNodes collection of the current element's parent."

For more info, check this
http://help.dottoro.com/ljanlxgl.php[^]
 
Share this answer
 
v2
Comments
RelicV 24-Apr-13 8:17am    
The issue with this kind of coding is its not dynamic. Based on your Left/Right align, you've to specify your Previous/Next Sibling. Not a bad one though, but check out solution 3. Every time a checkbox is rendered into HTML, it created 2 elements (input type=checkbox and its relevant label). This label will be placed either before or after the input checkbox.

cbh.parentElement.innerText would consider only the text for the entire elements.

The more your code is dynamic, the lesser the errors/issues you need to handle.

Bye the way, good job Naz_Firdouse.. :)

Thank you,
Vamsi
Naz_Firdouse 24-Apr-13 9:01am    
Thanks...
Hi v.sara,

Try this
JavaScript
cbh.parentElement.innerText

instead of
JavaScript
cbh.nextSibling.innerHTML


Please up-vote and Accept the answer if it helped you.!

Thank you,
Vamsi
 
Share this answer
 
Comments
v.sara 24-Apr-13 8:19am    
getting error as 'parentElement' is undefined .
RelicV 24-Apr-13 8:21am    
Let me know the Browser that your working on and version also. Thank you!
v.sara 24-Apr-13 8:50am    
IE 8
Naz_Firdouse 24-Apr-13 9:05am    
Using cbh.parentElement.innerText didn't give the exact value.
Here is my code
I have some controls in div defined in aspx page.
<div id="cssonclickswitch" class="cfix" runat="server">
Welcome
Hi
Bye
</div>

and I am adding checkbox in code behind as follows.
CheckBox chbxSelect = new CheckBox();
chbxSelect.ID = "chbx1" ;
chbxSelect.Checked = true;
chbxSelect.Attributes["onclick"] = "AttachmentsSelectionChange(this);";
chbxSelect.Text = "Hello";
chbxSelect.TextAlign = TextAlign.Left;
cssonclickswitch.Controls.Add(chbxSelect);

and my javascript function is
function AttachmentsSelectionChange(cbh) {
var UnCheckedfilename = cbh.previousSibling.innerHTML;
var val = cbh.parentElement.innerText;
}

when I click on checkbox,
the variable UnCheckedfilename contains "Hello" whereas
val contains "Welcome Hi Bye Hello" --- values of all controls in the div...
but this is not the requirenment...
Let me know if I am doing anything wrong...


I checked in IE10
RelicV 24-Apr-13 9:17am    
Try "cbh.name" in JS. I checked it in IE9/IE8 and FF. You should be fine with this.
var UnCheckedfilename = cbh.value;
gives value as On .
but var UnCheckedfilename = cbh.previousSibling.innerHTML;
Works Perfect .. thank you !
 
Share this answer
 
CHeckout sample on jsFiddle
http://jsfiddle.net/vijaypatel/ess84/[^]

You can get value of siblings using jQuery as below

HTML
<label for="chk1">checkbox 1</label>
<input type="checkbox" id="chk1" name="chk1" checked="checked" />


JavaScript
$('input').siblings('label').html();


From above code you will get "checkbox 1" as value from label.
 
Share this answer
 
you use
var UnCheckedfilename = cbh.value;
to solve your problem
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900