javascript - How to hide div after append to another? -
i've got kind of drop down menu dynamically appending differents divs. problem is, when click on "close", style.display = "none"
wont work. can change background, opacity, size cant hide it.
code looks this:
<style> html, body{ height: 98%; } #editorviewport{ width: 90%; height: 100%; min-width: 400px; min-height: 300px; position: relative; margin: 0 auto; border: 1px solid red; } #movingelementscontainer{ display: none; top: 0px; left: 0px; } #addstartingelementbtn{ width: 60px; height: 60px; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } #addstartingelementbtn:hover{ background-color: #c9eac6; border: 1px solid grey; cursor: pointer; } #elementsmenucontainer{ width: 150px; border: 1px solid grey; background-color: white; min-height: 100px; padding: 5px; position: absolute; z-index: 2; display: none; } .elementoption{ width: 90%; padding: 5px; border: 1px solid grey; } .elementoption:hover{ border: 1px solid red; cursor: pointer; } </style> <body> <div id="editorviewport"> <div id="addstartingelementbtn" data-owner="starting" data-side="starting" class="openelementsmenu"> click! </div> </div> <div id="movingelementscontainer"> <div id="elementsmenucontainer" data-open="false" data-owner="" data-side=""> <div data-kind="1" class="elementoption"> 1 </div> <div data-kind="2" class="elementoption"> 2 </div> <div data-kind="3" class="elementoption"> 3 </div> <div data-kind="99" class="elementoption"> close </div> </div> </div> </body> <script type="text/javascript"> function prepareeventhandlers(){ var openelementsmenu = document.getelementsbyclassname("openelementsmenu"); var event = window.attachevent ? 'onclick' : 'click'; for(var = 0; < openelementsmenu.length; i++){ if(openelementsmenu[i].addeventlistener){ openelementsmenu[i].addeventlistener('click', elementsmenu, false); }else{ openelementsmenu[i].attachevent('onclick', elementsmenu); } } var elementoption = document.getelementsbyclassname("elementoption"); for(var = 0; < elementoption.length; i++){ if(elementoption[i].addeventlistener){ elementoption[i].addeventlistener('click', selectelementtocreate, false); }else{ elementoption[i].attachevent('onclick', selectelementtocreate); } } } window.onload = function(){ prepareeventhandlers(); } var totalelements = 0; var editorviewport = "editorviewport"; var selectedelementid = ""; var elementsmenu = function(){ var elementsmenu = document.getelementbyid("elementsmenucontainer") this.appendchild(elementsmenu); elementsmenu.style.display = "block"; elementsmenu.style.left = 61 + "px"; elementsmenu.style.top = "0px"; elementsmenu.setattribute("data-open", "true"); elementsmenu.setattribute("data-owner", this.getattribute("data-owner")); elementsmenu.setattribute("data-side", this.getattribute("data-side")); } var selectelementtocreate = function(){ var datakind = this.getattribute('data-kind'); var parentnode = document.getelementbyid(this.parentnode.id); alert(datakind) if(datakind == "99"){ parentnode.style.display = "none" parentnode.setattribute("data-open", "false"); parentnode.setattribute("data-owner", ""); parentnode.setattribute("data-side", ""); } } </script>
here jsfiddle
many advise!
var selectelementtocreate = function(e){ var datakind = this.getattribute('data-kind'); var parentnode = document.getelementbyid(this.parentnode.id); alert(datakind) if(datakind == "99"){ console.log(parentnode); parentnode.style.display = "none" parentnode.setattribute("data-open", "false"); parentnode.setattribute("data-owner", ""); parentnode.setattribute("data-side", ""); alert("wont close :"); } e.stoppropagation(); }
Comments
Post a Comment