How can I keep a component dynamically created with jQuery on the page?

Asked

Viewed 69 times

0

How to keep an element dynamically created with jQuery on the page? When I refresh or return to the page, the element is removed.

  • 1

    You have to change your logic and not load the whole page. A refresh always clears HTML and Javascript.

  • Do you know how I can do that? I’m starting with js/jQuery.

  • Take a look here: https://answall.com/q/6626/129, it’s a good start

  • thanks, but it’s still not what I’m looking for. I’m using append to create cards materialize from the click of a page button.

  • And why you need to refresh the page?

  • is pq after created, the Component(card) needs to be saved on the page and it will be fed with information obtained via ajax, but when refreshing and changing page and coming back, the cards disappear, so wanted a way to keep them. cards can be created and deleted.

  • 1

    Why not send the data to recreate these "cards" by ajax as well? so when you open the page again you can recreate them

  • is a good idea! I’ll try this. obg.

  • This is one of the ideas (and usual solution) that is suggested in this other question I mentioned. So you know you have what’s important on the server and you can recreate in milliseconds when the page loads or the user goes back to the page. Like a "shopping cart" before it’s complete.

Show 4 more comments

1 answer

0

Using jQuery you can manipulate the element when creating the page, for example:

$(document).ready(function() {
    $("#minhaDiv").html('<div id="NovaDiv"></div>');
    $("#minhaDiv").append('<div id="OutraDiv"></div>');
});

Using pure javascript:

<script type="text/javascript">
   function carregarDados() {
      var divSelecionada = document.getElementById('minhaDiv');
      divSelecionada .innerHTML = '<ol><li>Lista de Exemplo</li></ol>';
   }
</script>

<body onload="carregarDados();">
    <div id="minhaDiv"></div>
</body>

Browser other questions tagged

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