Update content of DIV

Asked

Viewed 841 times

1

Is it possible to update the content of a DIV? I have a DIV in which I pick up data present in the browser cookies and make a foreach to the listing of the items of cookies showing them to the user. On this same page I have a button that when clicking calls a function adding another item to cookies, but if I open several browser tabs and add other items to cookies it does not make the updated listing of the current page. My intention is that when I click on the "Add Items" button it updates the already listed items and shows it to the user. Below cookie listing code for the user:

<div class="lista">
    <?php
        $myCookies = $this->getCookie('myCookies');
        foreach ($myCookies as $dados):?>
          <h3><?php echo $dados ?></h3>
    <?php endforeach; ?>
</div>

When I click the button <div class="add-itens" id="mydiv">Adicionar Itens</div> I would need to happen a refresh on div lista to update items already in cookies that have been added in other tabs.

To update via Jquery I tried this, but unsuccessfully.

$("#mydiv").load(location.href + "#mydiv");
  • 5

    Yes, using jquery + ajax. Anyway, add to the question what you have already done to make it easy to help in your specific case.

  • 2

    Possible duplicate of Change the contents of the div without refreshing the page, answers the question in the title, but does not answer the part about cookies and multiple tabs, I suggest an edition, explaining better this part of the problem

  • @Diegofelipe updated the question

1 answer

0

Solved

 $.ajax({
         url: "exemplo.php",
         type: 'GET',
         success: function(html){
             var headline = $(html).find('#mydiv');                                                     
             $('#mydiv').html(headline);
         }   
 });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.