views:

24

answers:

2

I have a ASP.Net TreeView Control with Checkboxes along Child nodes. I want to get Text of the checked Child Node in the TreeView control. And I want to get the checked Child Node Text using jQuery/javascript. Mostly I had used jQuery in the page I had done.

I had used $(this).text(). But its not working. Since the control is ASP.Net TreeView control and I am using jQuery. So either jQuery or javascript

A: 

First, you have to check if you are using adapters in the treeview. If you are using adapters, you basically have to search in the DOM generate the elements that have a class named ParentSelected, or Selected. That is the naming convention that microsoft uses.

It will be something like these $("li[class$='Selected']").children("a").val()

or

$("li[class$='Selected']").children("a").attr("text")

firematta
ok.what is this class$? is it default ?
Sreejesh Kumar
inside the for loop how will I retrieve the text ? is it $("li[class$='Selected']").children("a").attr("text") like you mentioned above ??? what is this class$ ?
Sreejesh Kumar
A: 

Since you didn't post a sample of what you're working with I created an example that tries to cover a few different scenarios.

<ul id="list">
  <li><input type='checkbox' id="check1" name="check1" value="hello" /> Checkbox #1</li>
  <li><input type='checkbox' id="check2" name="check2" /> Checkbox #2 <a href="#">hello</a></li>
  <li><input type='checkbox' id="check3" name="check3" /> Checkbox #3</li>
  <li><input type='checkbox' id="check4" name="check4"/> Checkbox #4</li>
</ul>

<button id="output"></button>

On button click...

$("#output").bind("click", function(){

  // can be any jQuery selector -- for this example we use #list 
  $("#list").find("input[type='checkbox']:checked").each(function(){
    var $t = $(this),           // current checkbox
        $p = $t.parent(),       // parent li - define more so w/ parent('li')
        text = $p.text(),       // text of li
        val = $t.val(),         // checkbox value
        id = $t.attr('id'),     // checkbox id
        name = $t.attr('name'), // checkbox name
        children = $p.children("a:first").text(); // select first child anchor element->get text

    // insert magical code here...

    // print to console for debug
    console.log($t, $p, text, val, id, name, children);

  });
});
Kieran