0

I am trying to display douter (my outer div) in the external JavaScript file but when I click on the button in my HTML page it does not show.i want to replace the div display with the outer div The only error that I see is this:

[HTTP/1.1 304 Not Modified 15ms]

Here's my HTML code:

<div id="display">
  <input type="button" id="button" value="Play Quiz" onclick="generator()"/>
</div>                    

... and here's my JavaScript:

function generator() {   
    button.style.display = "none";

    var douter = document.createElement("div");        
    douter.setAttribute("class", "douter");     
    douter.id = "douter";

    var dinner = document.createElement("div");   
    dinner.setAttribute("class", "dinner");     
    dinner.id = "dinner"; 

    var btn = document.createElement("button");      
    btn.innerHTML = "Next";
    // document.getElementById("display").style.display = "none";

    var rad = document.createElement("input");    
    rad.setAttribute("type", "radio");    
    rad.setAttribute("name", "rad");    
    rad.setAttribute("id", "rad");    
    rad.setAttribute("value", "rad");
    // document.write(Questions.question.length);

    douter.appendChild(dinner);    
    dinner.appendChild(rad);    
    dinner.appendChild(btn);    
}

I want to hide the display div and replace it with the outer div and its contents.

3
  • 2
    Welcome to Stack Overflow. I've fixed up the formatting in your post for you, but for future reference you can format your code by selecting your code then clicking the {} button, or indenting your code using 4 spaces. Thanks! Commented Feb 23, 2014 at 7:52
  • Have you looked at http/1.1 status code definitions? Commented Feb 23, 2014 at 7:56
  • 1
    where you appending your douter div finally? Commented Feb 23, 2014 at 7:56

2 Answers 2

1
dinner.appendChild(btn);
document.body.appendChild(douter);
} 

I think you missed to append created elements to body

Sign up to request clarification or add additional context in comments.

3 Comments

what should be done if the display div has to be replaced with outer div
document.getElementById("display").remove(); you can remove old by this way.
want to replace that with the outer div, tried yours but same output
0

You append the children but never actually append douter to the document.

Add this code at the end of your function...

If you want to simply add the new div:

document.appendChild(douter);

If you want it to replace the display div:

This assumes that displayDiv is at root (not inside another div)

var displayDiv = document.getElementById('display');
document.replaceChild(displayDiv, douter);

Now, if your display div is inside another div, then you need to get a reference to whatever div contains displayDiv. Since you didn't post more of your HTML structure, I'll post a quick example. Obviously you will need to make it work with your code.

Let's suppose this is your structure:

<div id="someComtainer">
    <div id="display"> <!-- ... Content ... --> </div>
</div>

Then you need a reference to the div with id "someContainer" to replace the display div:

var someContainer = document.getElementById('someContainer');
var displayDiv = document.getElementById('display');
someContainer.replaceChild(displayDiv, douter);

If you post your actual HTML I can make this more specific, but really this should give you enough to get your desired result.

Alternately, use style.display to show/hide two divs:

HTML:

<div id="message" style="display:none;"></div>
<div id="display"> <!-- ... Content ... --> </div>

JS:

var messageDiv = document.getElementById('message');
var displayDiv = document.getElementById('display');

messageDiv.appendChild(displayDiv, douter);

messageDiv.style.display = "block";
displayDiv.style.display = "none";

6 Comments

i want to hide the display div and only show the outer div and its contents @davidkhaykin
@user3342712: That is a good thing to post in your question for future reference. See my updated answer.
yeah i was looking out for this but it disappears and following error message is shown in the console TypeError: document.replaceChild.appendChild is not a function
@user3342712: That was a typo which I since fixed in my answer...it's just document.replaceChild(oldChild, newChild);
actually i need only a single div which should be replaced by another div i tried your code but it gives node not found
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.