0

Why are the inputs created by JavaScript in this very simple code appearing below the div instead of inside it?

HTML:

<div id="magicButtonDiv">
    <p>I am filler text</p>
</div>

JavaScript:

function makeButton() //Activated when a button in another part of the page is pressed
{
    var text = "";
    text += "<p>What is your gender?</p>";
    text += "<input type='button' class='genderButton' value='male' onClick='maleFunction'/>";
    text += "<input type='button' class='genderButton' value='female' onClick='femaleFunction'/>";
    document.getElementById("magicButtonDiv").innerHTML=text;
}

CSS:

#magicButtonDiv
{
    box-sizing         : border-box;
    -moz-box-sizing    : border-box;
    -webkit-box-sizing : border-box;
    border             : 1px solid #000000;
}

input.genderButton
{
    float.      : left;
    margin-left : 2%;
    width       : 47%;
}

There's more code, but I think the error is somewhere in this part of the code.

Bill Bllson
  • 179
  • 1
  • 1
  • 14
  • 1
    possible duplicate of [floating stuff within a div, floats outside of div. Why?](http://stackoverflow.com/questions/2062258/floating-stuff-within-a-div-floats-outside-of-div-why) – JJJ Aug 30 '15 at 17:31
  • you have many errors in your javascript..could you please add a working fiddle too.. – Lal Aug 30 '15 at 17:33

2 Answers2

1

Add style rule of float:left to your #magicbuttondiv. It will solve the problem

Ankit Agarwal
  • 1,350
  • 10
  • 15
0

See this fiddle

You have many errors in your Javascript. Please replace your Javascript with the below given one

function makeButton() {
    var text = "";
    text += "<p>What is your gender?</p>";
    text += "<input type='button' class='genderButton' value='male' onClick='maleFunction()'>";
    text += "<input type='button' class='genderButton' value='female' onClick='femaleFunction()'>";
    document.getElementById("magicButtonDiv").innerHTML = text;
}

UPDATE

I just found that you have errors in your CSS too..

Notice float.: left; specified for input.genderButton. Realised just now that the float was not actually applied.

So here is the updated fiddle with floats applied.

Lal
  • 14,726
  • 4
  • 45
  • 70