1

I created a <div> via javascript that has display:hidden; in its css. I want to make it a block element, but it won't appear. Here is my code.

var fatGooseShop = document.createElement('div');
            fatGooseShop.width = canvas.width / 1.5 + "px";
            fatGooseShop.height = canvas.height / 1.5 + "px";
            fatGooseShop.style.position = "absolute";
            fatGooseShop.style.left = "50%";
            fatGooseShop.style.top = "50%";
            fatGooseShop.style.margin = "-" + (canvas.height / 1.5) / 2 + "px 0px 0px -" + (canvas.width / 1.5) / 2 + "px";
            fatGooseShop.style.backgroundColor = "yellow";
            fatGooseShop.style.display = "none";
function shop()
{
    fatGooseShop.style.display = "block";
}

I call the shop() function from the browser to run it, if that makes a difference.

3
  • display: hidden or visibility: hidden? Commented Apr 8, 2012 at 20:50
  • have you defined the Canvas variable further up in the code? Commented Apr 8, 2012 at 21:14
  • 1
    hey guy.append your element into one element to show . Commented Apr 8, 2012 at 21:20

4 Answers 4

3
  1. Firstly You need to append the element

    document.getElementById("test").appendChild(fatGooseShop);
    
  2. There's no content and you're not actually setting the width, or height of the block, so it's not going to be visible. You need to modify your code as follows. Note: This will work, provided canvas.width is returning a non zero value

fatGooseShop.style.width = canvas.width / 1.5 + "px";
fatGooseShop.style.height = canvas.height / 1.5 + "px";

Example Here:

http://jsfiddle.net/DigitalBiscuits/cqbzw/6/

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

Comments

2

You need to append the element as well

x=document.getElementById("something");
x.appendChild(fatGooseShop);

1 Comment

Oh gosh.. I feel like such an idot for forgetting that. I fixed that part now, but when I run the shop() function it still doesn't show.
2

You create the element with createElement, but you need to appendChild it to another element in the DOM before it will appear. Once that's done you can manipulate it.

1 Comment

It is possible to manipulate it before appending, but it has no effect until it's appended.
2

You forgot to append it to the document. In your JS file add the line line below the //Javascript.

<!--HTML-->
<div id="the-div">

</div>

//Javascript
document.getElementById("the-div").appendChild(fatGooseShop);

Comments

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.