简体   繁体   中英

how to get the value to td inside list in javascript

I have a listview in asp.net it appears in html like this

<li id="l3" class="add" onclick="hilite()" style="">
   <table id="tbl" style="width: 100%;">
       <tbody>
          <tr style="width: 100%;">
              <td class="border2" style="width: 50%;"> 1011 </td>
              <td class="border2" style="width: 50%;"> Name 3 </td>
           </tr>
       </tbody>
    </table>
</li>

now i want to get all the table value via javascript on button click li contains table inside table tr and td presented i want td value of each list for sorting. and also i tried like this

function sortUnorderedList(ul, sortDescending) {
        if (typeof ul == "string")
            ul = document.getElementById(ul);
        var lis = ul.getElementsByTagName("li");
        var vals = [];
        for (var i = 0, l = lis.length; i < l; i++)
            vals.push(lis[i].innerHTML);
        vals.sort();
        if (sortDescending)
            vals.reverse();

        var contentArray = new Array();
        for (var i = 0, l = lis.length; i < l; i++) {
            lis[i].innerHTML = vals[i];

            alert(lis[i].innerHTML);

            var x = lis[i].innerHTML.getElementById('lsttable1').rows[0].cells;
            alert(x[0].innerHTML);
        }
 }

in alert(x[0].innerHTML); i got table then how should i take the td value of each li

This will display the value of each td in your list, using jquery.

$("ul li.add table td").each(function(){
   alert($(this).html());
});

If you need a specific list, you can use the id selector. To get the first row in the table, use the tr:first selector.

$("#l3 li.add table tr:first td").each(function(){
   alert($(this).html());
});

Take a look at the selector documentation for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM