0

I'm trying to add (and display) divs when a user presses a button, I'm also using css grids. I've had a look here and all the solutions advise using angular js or node js.

I've recently (last week) learnt html, css and javascript and I'm pressed for time hence I'm asking if is it possible to create these divs without angular js or node js?

8
  • 1
    Yes it is possible Commented Jun 20, 2018 at 14:00
  • yeah its just <div></div> Commented Jun 20, 2018 at 14:01
  • 4
    People were creating dynamic HTML long, long before Angular was even dreamed of. And Node really has nothing at all to do with it. Commented Jun 20, 2018 at 14:01
  • @L_Church I know that method I, that's hard coding it. I needed the user to be able to add columns to a grid I created at the press of a button. I got the answer. Commented Jun 20, 2018 at 14:09
  • html is not code it's markup Commented Jun 20, 2018 at 14:11

5 Answers 5

4

You can create HTML elements by using createElement

It's acutally pretty simple:

function add() {
  // Create element; can be whatever you want, e. g. div, h1, p, img...
  var div = document.createElement('div');
  
  // Set some attributes
  div.style.width = '200px';
  div.style.height = '200px';
  div.style.backgroundColor = 'red';
  
  // Append the div to the body
  document.body.appendChild(div);
}
  
  
<button onclick="add()">Add div</button>

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

1 Comment

@kuhle Glad to help! Please consider marking my answer as the one that helped you out.
2

There are lots of ways to do it.

  1. The obvious one is the mysteriously-named createElement, usually combined with appendChild and/or insertBefore:

    parent.appendChild(document.createElement("div"));
    
  2. You can replace the contents of a parent element by assigning a string containing HTML to its innerHTML:

    parent.innerHTML = "<div></div>";
    
  3. #2 replaces the parent's content. To add to it, you can use insertAdjacentHTML, again with a string containing HTML:

    parent.insertAdjacentHTML("beforeend", "<div></div>");
    

Lots to discover in MDN's DOM section.

1 Comment

Crowder, thank you so much. I'm learning a lot from that DOM section.
2

Yes, you can use document.createElement

Try this:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <script>    
            function CreateDiv(){
                var myDiv = document.createElement("DIV"); // Create a <div> node
                var myTextNode = document.createTextNode("Hello World"); // Create a text node
                myDiv.appendChild(myTextNode); // Append the text 

                //document.getElementById("myList").appendChild(node); // Append 
                document.body.appendChild(myDiv);
            }

        </script>
        <input type="button" onclick="CreateDiv();" value="Create div">
    </body>
</html>

Comments

1

You can use the native createElement() method.

var btn = document.createElement("BUTTON");        // Create a <button> element
var t = document.createTextNode("CLICK ME");       // Create a text node
btn.appendChild(t);                                // Append the text to <button>
document.body.appendChild(btn);                    // Append <button> to <body>

https://www.w3schools.com/jsref/met_document_createelement.asp

1 Comment

I appreciate the reference to the link. It helps.
0

<!DOCTYPE html>
<html>
 <head>
 </head>
<body>
  <div class="wrap">
    <button class="btn"
    onclick="document.getElementById('text').innerHTML = 'Hello'">
    Click On Me</button>
    <p id="text"></p>
 </div> 
</body>
</html>

Check this one.

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.