Bhavesh G
Bhavesh G

Reputation: 3028

jquery : call function for dynamic created elements

I want to call this after page load: $('#chatscroller').tinyscrollbar() Here the #chatscroller is the dynamically appended div block.

HTML ( Existing code block ):

<div id="onlinechatlist">
    <div class="chatimage">
    </div>
    <div id="chatscroller">
    </div>
</div>

JAVASCRIPT :

function tinyscrollbar( ) {
    // some codes for scrolling the particular division
}

function appendcode ( ) {
    $("#onlinechatlist").append(append above html code);
    // code will appended at the last of the page
});

The problem is that when the code is appended to the end of the page, the function ( here : tinyscrollbar() ) bound with appended code's class is not executed.

Upvotes: 2

Views: 1290

Answers (1)

kontur
kontur

Reputation: 5220

Not sure if I really do understand your question right, but maybe this helps?

This waits for the page to load, then appends the html, then calls the tinyscrollbar function.

var chatscrollerhtml = '<div id="chatscroller">blabla</div>';

function tinyscrollbar() {
  //....
}

$(document).ready( function() {
  $(body).append(chatscrollerhtml);
  $('#chatscroller').tinyscrollbar();
});

Upvotes: 1

Related Questions