-4

I wish to dynamically add a button on a html form so that a user can upload multiple files when filling-in the form. The css design is both elastic and fluid and the intention is that whenever a user clicks on the "add file" link, a new button is added to allow the user to browse for the file, add it and finally upload it. The buttons should appear on the main content area of the page and should not overflow outside the footer area and should be rendered vertically with each button appearing on a newline. The code below does exactly that. This question is very much related to this question but with a twist of the css (design) requirements.

    
        function myFunction() {
 var btn = document.createElement("BUTTON");
 var text = document.createTextNode("Browse..");
 btn.appendChild(text);
 var newbutton = document.getElementById("button"); //new div button   element introduced on html.
   document.body.insertBefore(btn,newbutton);//insert before method
 
}
    
    /*Mailu*/
        div {
     padding: 1px 0;
       }
        #header {
         background: #DDDDDD;
         
        }
    
        #footer {
         clear: both;
         background: #DDDDDD;
        }
    /*add css as suggested here https://stackoverflow.com/a/2038374/2941758*/
        button{display:block;}
    
    
    
        <!DOCTYPE html>
        <html>
        <body>
        <link href="button.css" rel="stylesheet" type="text/css"/>
        <div id = "header">
        <p>Home</p>
        </div>
    
    
        <p>Upload file.</p>
        First name: <input type = "text" name = "Firstname"><p>
        <div id = "button"> <!--added div button on html-->
        <a href="javascript:void(0)" onclick= "myFunction()" > Add file</a>
        </div>
        <div id="footer"><p>footer</p>
    
        </div>
    
    
    
        </body>
        </html>
    
    
  [1]: https://stackoverflow.com/questions/29943352/dynamically-create-element-but-use-a-href-instead-of-a-button-onclick
Community
  • 1
  • 1
Maina Mailu
  • 73
  • 3
  • 13
  • You're mixing some concepts here. No element is "within" the CSS, only rules about the display of your document. If something doesn't have this rule applied, then the rule target ("css selector") is specified incorrectly. (hint: # is for specifying target id in CSS) – doldt Jun 11 '15 at 08:00
  • _"outside the CSS"_? There is no such thing... – Cerbrus Jun 11 '15 at 08:03
  • @doldt even if i add the elements within the css, the results remain the same – Maina Mailu Jun 11 '15 at 08:03
  • @AlexMaina, as I said, you cant add elements to the css. Only *selectors* which will *match* certain elements. – doldt Jun 11 '15 at 08:07
  • I have edited the css above and included id div @doldt – Maina Mailu Jun 11 '15 at 08:15
  • After editing the code above, the javascript puts all the buttons before the footer. How can i now make the buttons appear on a new line without them showing after the footer? – Maina Mailu Jun 11 '15 at 17:32
  • @Cerbus... i have found an answer. Please open up the question from "on hold" so that i can answer it – Maina Mailu Jun 12 '15 at 12:22
  • The answer to this problem lies here http://stackoverflow.com/a/2038374/2941758 – Maina Mailu Jun 12 '15 at 12:29

1 Answers1

1

Replace:

#button {

With:

button {

The # is an id selector, meaning it will apply the styles to the element that has id="button".

If you leave out the #, it will match all <button> elements.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • @Cerbrus...done that. result remains the same – Maina Mailu Jun 11 '15 at 08:09
  • Sure, but the rule _is_ now applied to your dynamically created button. If the `margin-left` isn't changing, it's because it already was `auto`. What exactly are you trying to do to that button? – Cerbrus Jun 11 '15 at 08:11
  • I'm trying to upload a file with it. I need it to fall within the CSS. I have editd the CSS above and ncluded id div. @Cerbrus – Maina Mailu Jun 11 '15 at 08:14
  • What do you mean with _"I need it to fall within the CSS"_? – Cerbrus Jun 11 '15 at 08:17
  • I want the button to appear before the footer not after – Maina Mailu Jun 11 '15 at 08:44
  • Oooooooh. That's a _completely_ different question. This could help: http://stackoverflow.com/questions/7258185/javascript-append-child-after-element – Cerbrus Jun 11 '15 at 08:45
  • @Cerbus i have found a solution here http://stackoverflow.com/questions/2038369/css-how-to-place-a-submit-button-on-a-new-line/2038374#2038374...thanks for the support – Maina Mailu Jun 12 '15 at 12:48