Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
This is my code .
chbxSelect = new CheckBox();
                chbxSelect.ID = "chbx" + i.ToString(); 
                chbxSelect.Checked = true;
  chbxSelect.Attributes["onclick"] = "AttachmentsSelectionChange(this);";
                chbxSelect.Text = fileName.ToString();
                chbxSelect.TextAlign = TextAlign.Left;
 
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 24-Apr-13 2:02am
v.sara407
Comments
ryanb31 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you set TextAlign to Left, then you can access the value by setting
 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[^]
  Permalink  
v2
Comments
mvamsianil at 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 at 24-Apr-13 9:01am
   
Thanks...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi v.sara,
 
Try this
cbh.parentElement.innerText
instead of
cbh.nextSibling.innerHTML
 
Please up-vote and Accept the answer if it helped you.!
 
Thank you,
Vamsi
  Permalink  
Comments
v.sara at 24-Apr-13 8:19am
   
getting error as 'parentElement' is undefined .
mvamsianil at 24-Apr-13 8:21am
   
Let me know the Browser that your working on and version also. Thank you!
v.sara at 24-Apr-13 8:50am
   
IE 8
Naz_Firdouse at 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
mvamsianil at 24-Apr-13 9:17am
   
Try "cbh.name" in JS. I checked it in IE9/IE8 and FF. You should be fine with this.
Naz_Firdouse at 24-Apr-13 9:25am
   
no result. name gives the ID... not the text value in IE10,IE9 and IE8
mvamsianil at 24-Apr-13 9:48am
   
Hmm. Actually, I lost somewhere with my work and got confused. Sorry about that..
 
if (cbh.parentElement.children.length == 2) {
if (cbh.parentElement.children[0].type == "checkbox") {
alert(cbh.parentElement.children[1].innerHTML);
}
else {
alert(cbh.parentElement.children[0].innerHTML);
}
}
 
Thank you and sorry for misleading with my inappropriate actions.
Naz_Firdouse at 25-Apr-13 1:23am
   
This works perfectly only if I add my checkbox to a div(container)...
means one checkbox for one div...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

var UnCheckedfilename = cbh.value;
gives value as On .
but var UnCheckedfilename = cbh.previousSibling.innerHTML;
Works Perfect .. thank you !
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

CHeckout sample on jsFiddle
http://jsfiddle.net/vijaypatel/ess84/[^]
 
You can get value of siblings using jQuery as below
 
    <label for="chk1">checkbox 1</label>
    <input type="checkbox" id="chk1" name="chk1" checked="checked" /> 
 
$('input').siblings('label').html();
 
From above code you will get "checkbox 1" as value from label.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you use
var UnCheckedfilename = cbh.value;
to solve your problem
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 24 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100