I want to do some operation by taking an ID of a Task List item in SharePoint on UI front. I've just wrote some jQuery code by traversing the element. I got the ID and performed operations what I wanted to do. But I think this is not best practices to do so.

$(document).on("click", "#idHomePageEditItem", function () {
   $("#Hero-WPQ2").next().next().next().children("tbody").children().each(function () {
      if ($(this).hasClass("s4-itm-selected")) {
         var selectedRecordId = $(this).children().eq(1).children().children().children().children().attr("liid");    
         alert(selectedRecordId );

If you run this code, then you can get id of the taskList item in the selectedRecordId variable.

| improve this question | | | | |
  • \$\begingroup\$ Can you add the html for 'Hero-WPQ2'? \$\endgroup\$ – RobH Mar 31 '15 at 10:47
  • \$\begingroup\$ 'Hero-WPQ2' is id of whole table of task list which includes New Item and Edit links also in OOB \$\endgroup\$ – user3022326 Mar 31 '15 at 11:08
  • 3
    \$\begingroup\$ The problem is that without knowing the html I can't suggest a better way of traversing the elements. In general seeing .children().children().children() is a bad sign. \$\endgroup\$ – RobH Mar 31 '15 at 11:33
  • \$\begingroup\$ Ya I know its very bad sign that taking some element, traverse to some DOM and get work done. But If I not take this then ultimatly I have no other option to get selected item from task list. Or else I need to add one more seperate column in list which hold itemId of listItem. Any other way to get selected item? 'Hero-WPQ2' is default id of List View web part's table in SharePoint for any list. \$\endgroup\$ – user3022326 Mar 31 '15 at 11:53
  • \$\begingroup\$ Have you looked into JSLink/Client Side Rendering at all? \$\endgroup\$ – Thriggle Jan 17 '17 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.