agottschling
agottschling

Reputation: 15

Javascript/Jquery Uncaught typeerror trying to replace html

trying to remove some text on my site using jQuery after the page has finished loading, and the code isn't working. When looking at the JS console in Chrome, I get the following error Uncaught TypeError: Cannot read property 'replace' of undefined.

Here's my code for replacing the text:

<script>
window.onload = function(){
  var replaced = $("footer-copy").html().replace("'s Frontpage",'');
  $("footer-copy").html(replaced);
};
</script>

And the HTML I'm trying to update:

<div class="footer-wrap">
    <div class="footer-copy">© 2017 <a href="https://blog.andrewgottschling.me/">Andrew Gottschling's Frontpage</a> Powered by <a href="https://leafpub.org" target="_blank">Leafpub</a></div>
    <div class="footer-design-author"><span>Design with</span> <i class="i-favorite heart"></i> by <a href="https://leafpub.org" target="_blank" title="Desarrollador Web FullStack.">@leafpub</a></div>
</div>

Thanks for all the help in advance. :D

Upvotes: 0

Views: 71

Answers (3)

Wellington Junior
Wellington Junior

Reputation: 1

Try to use the class selector "." before the class name. Like this:

<script>
window.onload = function(){
  var replaced = $(".footer-copy").html().replace("'s Frontpage",'');
  $(".footer-copy").html(replaced);
};
</script>

Upvotes: 0

Zailef
Zailef

Reputation: 715

You need to add a class selector, otherwise jQuery searches for a tag with that name.

var replaced = $(".footer-copy").html().replace("'s Frontpage",'');

Notice the dot before the class name.

Upvotes: 4

hsz
hsz

Reputation: 152206

To find an element using class name, you need prepend this class with dot:

$(".footer-copy")

Upvotes: 2

Related Questions