javascriptgetelementbyid

'innerHTML' doesn't work


I have currently this line in my page :

<p id="replaced">
    <a id="linkReplace" href="https://www.g2a.com/r/nowyoushare">
        <img src="images/ads.png" class="img-responsive" alt="" onmouseover="hover(this);" onmouseout="unhover(this);">
    </a>
</p>

So adblock detect and block my link and picture: image

So a created a test script with JavaScript but it doesn't work :/

Code :

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/lightbox.min.js"></script>
<script type="text/javascript" src="js/wow.min.js"></script>
<script type="text/javascript" src="js/main.js"></script> 
<script>
    if(document.getElementById('replaced').length == 0) {
        document.getElementById('replaced').innerHTML = 'chocolat';
    }
</script>

Can someone help me ? Thanks


Solution

  • But I want that if "replaced" is empty so add some content into

    If you want to check if the content is empty so use .innerHTML.length :

    if(document.getElementById('replaced').innerHTML.length == 0) {
        document.getElementById('replaced').innerHTML = 'chocolat';
    }
    

    Hope this helps.


    Empty content case :

    if(document.getElementById('replaced').innerHTML.length == 0) {
      document.getElementById('replaced').innerHTML = 'chocolat';
    }
    <p id="replaced"></p>