Search code examples
javascriptjqueryloadappendpreventdefault

jQuery append is not working when using load before


Append function is not working when using it after the load function.

$('section article a').on('click',function(e){

    e.preventDefault();
    id = $(this).attr('class');
    $('article[id='+id+']').load( 'articles/'+id+'.html' );

    $('article[id='+id+']').append( "<strong> Where am I?<br/>StackOverflow HELP !!!</strong>" );   //This is NOT appended

});

Any idea ?


Solution

  • Try to wait until your load finish then use append() instead:

    $('article[id='+id+']').load( 'articles/'+id+'.html', function() {
        $('article[id='+id+']').append( "<strong> Where am I?<br/>StackOverflow HELP !!!</strong>" ); 
    });
    

    Also you can just use $('#'+id) instead of $('article[id='+id+']') since id is unique.