简体   繁体   中英

get a value from an element and add to the end of each href

There is a table that consist of many table rows with columns.

and I'd take a value from each td with classname: routecode and I have to add that value to the end of closest href of each tr. I would add this to the value of routecode link.

I wrote the below code but it does not work and the value is not added to the end of link.

Here is my code:

 $(".list").each(function(index, element) { var routecode = $(this).closest("tr").find(".routecode").text() var linkk = $(this).attr("href") $(this).attr("href", linkk + routecode); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <html> <head> </head> <body> <table border="1"> <tr class=" odd"> <td class="routecode">TK26</td> <td> <a class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a> </td> </tr> <tr class=" odd"> <td class="routecode">tkeee</td> <td> <a class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a> </td> </tr> </table> </body> </html> 

You can try this code:

You are missing semicolon ; end of line..

Demo here: https://output.jsbin.com/nolomec

https://jsbin.com/nolomec/2/edit?html

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $(".list").each(function(index, element) {
    var routecode = $(this).closest("tr").find(".routecode").text();
    var linkk = $(this).attr("href");
    $(this).attr("href", linkk + routecode);
  });
});
</script>
</head>
<body>

<table border="1">
<tr class="odd">
<td class="routecode">TK26</td>
<td>                                       
<a class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a>
</td>                                   
</tr>

<tr class=" odd">
<td class="routecode">tkeee</td>
 <td>                                       
<a  class="list" href="/reservation/route/info.bc?dmnid=2893&routecode=">Edit </a>
</td>                                   
</tr>                             
</table>

</body>
</html>

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