Ajay K Ajay K - 2 months ago 13
jQuery Question

unable to find closet label text in JQuery

i want to access label text which is nearby checkbox. i am visual stdio and C#.

HTML PAGE

<td><input id="ctl00_ContentPlaceHolder1_CHKBranches_0" type="checkbox"
name="ctl00$ContentPlaceHolder1$CHKBranches$0">
<label for="ctl00_ContentPlaceHolder1_CHKBranches_0">Kolkatta (EJAA)</label></td>


jquery script for access text

var strBr = "";
$("input[id*='CHKBranches']").each(function() {
if ($(this).is(':checked')) {
// Note : successfully check checkbox but unable to find closet label text
strBr += $(this).closest('label').text() +",";
}
});

Answer

Why not just move up to the parent and then find the label?

jQuery

var strBr = ""; 
  $("input[id*='CHKBranches']").each(function() {
    if ($(this).is(':checked')) {                   
      var $parent = $(this).parent();               
      strBr += $parent.closest('label').text() +",";
    }
  });
Comments