I have buttons that are dynamically created , let say 5 buttons gets created button1, button2, button3, button4, button5. when I click on some button(let say, button1) a div(say div1) is created. And when i click on other button(say button2) another div(say div2) is created. I want to display one at a time. That is, when i click on button1 , div1 should be displayed and rest other div should not be displayed. But in my code when i click on button1, div1 gets created. And when i click on button2 div2 is appended. I want to replace div2 by div1.
I have tried using replaceWith() and checking for siblings also. But it didn't worked.
Here is my code:
javascript:
$(document).ready(function(){
var request = $.ajax({'url': '/FunctionName'});
request.done(function(response)
{
var output = document.getElementById('output');
response = JSON.parse(response)
response.forEach(function(item){
var button = document.createElement("input");
button.type = "button";
button.value = item[0];
button.name= item[0];
button.onclick = createDiv;
output.appendChild(button);
function createDiv() {
var div = document.createElement('div');
div.setAttribute("id", item[0]);
div.setAttribute("class","inner");
div.innerHTML = item[1];
showDiv.appendChild(div);
}
});
});
request.fail(function(jqXHR, textStatus)
{
alert('Request failed: ' + textStatus);
});
});
HTML:
<div id="regex_1" class="tab-pane fade">
<div id="output" class="panel panel-default" style="float:left; width: 30%" >
</div>
<div id="showDiv" class="panel panel-default" style="float:left; width: 70%" >
</div>
</div>