Visual
Visual

Reputation: 33

trying to find the text and change the anchor tag through html failing

I have the following javascript inside a div, and trying to find it to replace a complete a tag with new type of a tag

Here is the html i have

<div class="info-more"><h3>We're here to help!</h3><p>Sales: 1-800-555-5555</p><p>Support:&nbsp;1-888-555-6655</p><a href="https://website.com" target="_blank">Click here to chat with us</a></div>


$(document).ready(function() {
    console.log($(document).find(".info-more").text());
    $(".info-more").closest("a").attr("href","javascript:;");
});

Upvotes: 0

Views: 56

Answers (1)

Nick Parsons
Nick Parsons

Reputation: 50664

You can use .find() instead of .closest(). Find will look at descendants (ie children of the selector element), whereas, closest will look only at the parents of the selector element as it traverses up through its ancestors to find the selector.

See working example below:

$(document).ready(function() {
  console.log($(".info-more").find("a").text());
  $(".info-more").find("a").attr("href", "javascript:;");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="info-more">
  <h3>We're here to help!</h3>
  <p>Sales: 1-800-555-5555</p>
  <p>Support:&nbsp;1-888-555-6655</p>
  <a href="https://website.com" target="_blank">Click here to chat with us</a>
</div>

Upvotes: 1

Related Questions