user1339913
user1339913

Reputation: 1027

replace the value of anchor tags in jquery

I have the following html I need to go through each anchor tag and check if the anchor tag value is APL

$('a').text()="APL"

If value is equal to APL i need to remove the hyperlink on the Status column.

Is there i can loop through all the anchor tags and remove the hyperlink tag

<table>
  <tbody>
    <tr>

      <th scope="col">Id</th>
      <th scope="col">Class</th>
      <th scope="col">Status</th>


    </tr>
    <tr>
      <td>185</td>
      <td>1</td>
      <td>
        <DETAIL</td>
          <td><a href="Detail.aspx?id=185" target="_blank">APL</a>
          </td>
    </tr>
    <tr>
      <td>186</td>
      <td>2</td>
      <td>
        <DETAIL</td>
          <td><a href="Detail.aspx?id=185" target="_blank">DDL</a>
          </td>
    </tr>
    
  </tbody>
</table>

Upvotes: 3

Views: 61

Answers (2)

Raoulito
Raoulito

Reputation: 361

$('a') is maybe quite a large selector for such a function, but the following works.

$('a').each(function(){
    if ( $(this).text() == 'APL' ) {
        $(this).removeAttr('href');
    }
});

https://jsfiddle.net/v8qr2vgL/

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82231

You need to use

1) filter function to get anchor elements having text as APL.

2) And .removeAttr() for removing attribute href

$(function(){
  $('a').filter(function(){
    return $(this).text().trim() === "APL" ;
  }).removeAttr("href");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
  <tbody>
    <tr>

      <th scope="col">Id</th>
      <th scope="col">Class</th>
      <th scope="col">Status</th>


    </tr>
    <tr>
      <td>185</td>
      <td>1</td>
      <td>
        <DETAIL</td>
          <td><a href="Detail.aspx?id=185" target="_blank">APL</a>
          </td>
    </tr>
    <tr>
      <td>186</td>
      <td>2</td>
      <td>
        <DETAIL</td>
          <td><a href="Detail.aspx?id=185" target="_blank">DDL</a>
          </td>
    </tr>
    
  </tbody>
</table>

Upvotes: 5

Related Questions