krish mandava
krish mandava

Reputation: 83

Dom manipulation not working

I wrote the below code to remove an element with class rc-anchor-pt (if it is present in the DOM) after 5 seconds,

checkContainer();

counter = 1;
function checkContainer () {

    alert("checkContainer");
    $('.rc-anchor-pt').remove();
    $('.rc-anchor-logo-portrait').append('<a href=\"http://www.un.org/en/aboutun/privacy/\" target=\"_blank\">Privacy &amp; Terms</a>');
      if($('.rc-anchor-pt').is(':visible')){ //if the container is visible on the page
          var privacy = $('.rc-anchor-pt').find('a');

              } else {
          if (counter === 1)
         {
            setTimeout(checkContainer, 5000); //wait 50000 ms, then try again
            counter++;
         }
      }
 }

But the below line is not removing the element from the DOM. Can you please tell me what is the reason. Thanks in advance.I am running inside document.ready only The element is present in the page –

$('.rc-anchor-pt').remove();

Upvotes: 0

Views: 493

Answers (1)

A.Sharma
A.Sharma

Reputation: 2799

I am not really sure what you are trying to accomplish with your code. You have stated in your question that you wish to remove an element from the DOM after 5 seconds...You should be able to accomplish that with the following code:

$('.rc-anchor-logo-portrait').append('<br><a href=\"http://www.un.org/en/aboutun/privacy/\" target=\"_blank\">Privacy &amp; Terms</a>');  

setTimeout(function(){
     $('.rc-anchor-pt').remove();
}, 5000); 

The way you have your code laid out, the rc-anchor-pt class will never be visible. It would really have no purpose then. If you want the append function to run after 5 seconds as well, just put it in the setTimeout function.

Here is a fiddle: https://jsfiddle.net/1399u65t/3/

Upvotes: 1

Related Questions