0

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

Shahzad Ahamad
  • 809
  • 1
  • 11
  • 30

1 Answers1

1

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);
});
Bhagwan Parge
  • 382
  • 1
  • 11
  • Please take a moment to edit your question and explain what you've done differently (and perhaps *why*). A "spot-the-difference" answer is very rarely a "good" answer. Additionally, no need to encourage other users to upvote - comments should remain helpful and constructive :) – Tyler Roper Sep 13 '18 at 16:00
  • @bhagwanparge I am out of the office will check it tommorow and let know if it works – Shahzad Ahamad Sep 13 '18 at 17:56