简体   繁体   中英

Creating Dynamic HTML with Javascript

I have been trying to dynamically append HTML input forms using a JS function to a webpage but am having trouble inserting them into the correct location. I have tried to create an empty div tag on line 40:

<div class="itemInfo"></div>

However, when I try to locate that tag from within my JS function, with the findElementById() function it seems not to find it. I want to create a copy of the three input forms above the empty tag I created and append them underneath, but no matter what I have tried I have not been able to figure out how to put the forms in that exact location. My JS function is as follows:

function newItem(){
  instance++;
  var oldInput = document.getElementById("itemInfo");
  var newDiv = document.createElement("INPUT");
  newDiv.name = "myinput";
  newDiv.value = "Enter Here";
  oldInput.insertBefore(newDiv);
}

Rather than creating the forms again in this function, I would like to duplicate the following and simply append it after itself:

<p>Item: <input type="text" name="item"/> 
   Qty: <input type="text" name="qty"/>
   Color: <input type="text" name="color"/></p>
    <input type ="button" value="Add Item" onclick="newItem();"/>
    <div class="itemInfo"></div>

I tried to wrap the three forms in a tag and calling that by Id, but it did not seem to work either, which is why I tried to make an empty tag after it. I have searched everywhere and there is a lot of information regarding similar issues but I can't seem to apply the solutions to my situation. I really appreciate any help. Here is the entire page:

    <!DOCTYPE html>
<html>
  <head>
    <script type ="text/javascript">
      var instance = 0;

    function newItem(){
      instance++;
      var oldInput = document.getElementById("itemInfo");
      var newDiv = document.createElement("INPUT");
      newDiv.name = "myinput";
      newDiv.value = "Enter Here";
      oldInput.insertBefore(newDiv);
    }

    </script>
    <title>Welcome to New Age Embroidery!</title>
    <style type="text/css">
     body {font-family:sans-serif;color:#4f494f;}
     form input {border-radius: 7.5px;}
     h5 {display: inline;}
     .label {text-align: right}
     .ordersBook {float:left; padding-top: 10px;}
     .name {width:100%;float:left; padding:3px;}
     .wrapper { padding-left: 25px; padding-top: 20px}
    </style>
   </head>
   <body>
   <input type ="button" id="btnAdd" value="Add Item" onclick="newItem();"/>
    <div class = "wrapper">
     <h1>Welcome to New Age Embroidery!</h1>
     <div class="ordersBook_input">
     <form method ="post" class="form" action = "/newguest" method = 'post'>
       Name: <input type="text" name="name"/>

       <p>Item: <input type="text" name="item"/> 
       Qty: <input type="text" name="qty"/>
       Color: <input type="text" name="color"/></p>
        <input type ="button" value="Add Item" onclick="newItem();"/>
        <div class="itemInfo"></div>

       <p>Phone: <input type="text" name="phone"/>
       Email: <input type="text" name="email"/>
       Artwork: <input type="file" name="file"/>
       <p>Quote: <input type="text" name="quote"/></p>
     </p>
       <p>Notes: <textarea cols="40" rows="10"></textarea>
     </p>
       <input type="submit" value='Add Order'/>
     </form>
     </div>
     <div class ="ordersBook">
      <h3>Orders</h3>
      %for name in mynames:
      <div class="name">
      <h5>Name:</h5> {{name['name']}}
       <h5>Phone:</h5>{{name['phone']}}
       <h5>Email:</h5>{{name['email']}}
       <form action="/view/{{name['_id']}}" method='GET' ><input type="submit" value="View">
       </form>
       <form action="/remove/{{name['_id']}}" method='POST'> <input type="submit" value="Remove" onClick="confirm('Are you sure you want to permenantly remove this order?')">

       </form>
     </div>
     %end
     </div>
    </div>
   </body>
</html>

I changed your Javascript to this

function newItem(){  
     newInput="<p>Item: <input type="+"text"+" name="+"item"+"/></p>";
     document.getElementById("itemInfo").innerHTML=newInput;
   }

Here's a working FIDDLE

You are trying to use the function document.getElementById("itemInfo"); which looks for the id itemInfo . There is no element with the id itemInfo in your page. Create the div as follows:

<div class="itemInfo" id="itemInfo"></div>

This should help you get a reference of the div element.

EDIT: The Error is in the function, node.insertBefore(new Element,reference Element); is the correct function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM