I want to hide and show div's that are created dynamically. I create the divs by pushing the button Category.
<button id="Category">Add category</button> And then use jquery to add that div to the DOM with the following function, so every time i want to hide and show this div I just click the button.
$(document).ready(function () { $("#Category").click(function () { var categoria = prompt("Introduce el nombre de la categoria"); nombreCategoria = categoria; $("#Productos").append('<div id=' + '"' + categoria + '"' + '><br><button onclick=' + '"' + category+ '()"' + '>' + categoria + '</button></div>'); }); }); But my problem is i don't know how to get the id of the dynamically created div, so that every time I push click on the name of any create div it would hide or show.
I used the following function to hide and show a div that was already created, but this doesn't work on the divs that are created dynamically.
function category() { var x = document.getElementById("ordenadores"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } Any help would be appreciated.