Search code examples
phpjqueryload

Reload a div without creating a nested one with jquery


I was trying to reload a div with jquery using the following code :

function reload(param)
{
$("#divId"+param).load(location.href+" #divId"+param);
}

Unfortunately, a nested div with the same id is then created inside the first one, causing it hard to delete. I tried this :

$("#divId"+param).load(location.href+" #divId"+param+">*","");

but then the whole div content is deleted.

Here is my div :

<div id="divId">

Random number : <?php echo rand(1,10); ?>

</div>

Solution

  • The reason why this is not working is because your div is empty of tags, so no elements were found inside your div ; try by adding a tag to your text - for instance :

    <div id="divId">
    <p>
    Random number : <?php echo rand(1,10); ?>
    </p>
    </div>
    

    Your div should then be correctly reloading when launching reload("");