-3

I am new in Jquery. I want to retrieve the data value of anchor tag. i.e (88997755 and 11223344) also the value in href attribute i.e(123456,45678). I tried multiple times but am not able to get the these values. I want to set either of the value in id attribute of the third "". need help.

<div class="body" id="List_body">
<table class="list" border="0" cellspacing="0" cellpadding="0">
   <tbody>
      <tr class="headerRow">
         <th class="actionColumn">Action</th>
         <th scope="col" class="idclass">Id</th>
         <th scope="col" class="number">NUmber</th>
      </tr>
      <!-- ListRow -->
      <tr class="dataRow" ">
         <td class="actionColumn"></td>
         <th scope="row" class=" dataCell">
            <a href="/123456" class="commentItem">88997755</a>
         </th>
         <td  class=" dataCell" id="set id 88997755 here using Jquery"> 20</td>
      </tr>
      <!-- ListRow -->
      <tr class="dataRow">
         <td class="actionColumn"></td>
         <th scope="row" class=" dataCell">
            <a href="/45678" class="commentItem">11223344</a>
         </th>
         <td class="dataCell-" id="set id 11223344 here using Jquery" >20</td>
      </tr>
   </tbody>
</table>
</div>
Jay
  • 3
  • 2
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Mar 16 '18 at 09:48
  • 1
    Possible duplicate of [jquery: get value of custom attribute](https://stackoverflow.com/questions/7177512/jquery-get-value-of-custom-attribute) – Waqas Bukhary Mar 16 '18 at 09:57

1 Answers1

0

Try this code.

$(document).ready(function(){
$(".list").on("click","a",function(){
    var href = $(this).attr("href");
    var text = $(this).text();

    $(this).parents("tr.dataRow").find("td.dataCell").attr("id",text); //set text as a id of td

    });
});

Update


$(document).ready(function(){
    $(".list a").each(function(){
        var href = $(this).attr("href");
        var text = $(this).text();

        $(this).parents("tr.dataRow").find("td.dataCell").attr("id",text); //set text as a id of td

        });
    });
Smit Raval
  • 3,385
  • 1
  • 7
  • 23
  • I dont want these id's to set on click event. can we set id when page is ready. – Jay Mar 16 '18 at 10:00
  • thanks Smit !!! I modified the code as per my requirement. Updated code works for me ! – Jay Mar 19 '18 at 14:21