-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
8
  • 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) Commented Jun 11, 2015 at 8:00
  • "outside the CSS"? There is no such thing... Commented Jun 11, 2015 at 8:03
  • @doldt even if i add the elements within the css, the results remain the same Commented Jun 11, 2015 at 8:03
  • @AlexMaina, as I said, you cant add elements to the css. Only selectors which will match certain elements. Commented Jun 11, 2015 at 8:07
  • I have edited the css above and included id div @doldt Commented Jun 11, 2015 at 8:15

1 Answer 1

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.

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

7 Comments

@Cerbrus...done that. result remains the same
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?
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
What do you mean with "I need it to fall within the CSS"?
I want the button to appear before the footer not after
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.