Shahzad Ahamad
Shahzad Ahamad

Reputation: 829

append in jquery works sometimes and sometimes doesn't

I am trying to append a span element in a div tag using the below code .

 $( document ).ready(function() { 
$(".rocketchat-widget").append("<span class='tooltiptext'>Chat button</span>");
}); 

sometimes it works and adds the element.

enter image description here

But sometimes it doesn't load the element. enter image description here

I am new to jquery so any help would be appreciable

Thank you

Upvotes: 0

Views: 163

Answers (1)

Bhagwan Parge
Bhagwan Parge

Reputation: 382

Below is what you want, op (shahzad) was including third party widget on page load, as it is a third party widget sometimes it will load early or later, so, needed below code,

$( document ).ready(function() { 
    $(document).on("click", "[data-state=closed]" , function() { $(this).data('state', 'opened').attr('data-state', 'opened'); }); 

    myVar = setInterval(function() {
        if($(".rocketchat-widget").length){
            $(".rocketchat-widget").append("<span class='tooltiptext'>Chat button</span>");
            clearInterval(myVar);
        }        
    }, 1000);
});

Upvotes: 1

Related Questions