0

I'm having trouble, grabbing the user input, and having the onclick operator create additional paragraphs with each click.

Here is my HTML code.

    <!DOCTYPE html>

    <html lang='en'>
    <head>
    <title>Add Paragraph </title>
    <meta charset='utf-8' >
    <script src="../js/addPara.js" type="text/javascript"></script>
    </head>  
    <body>
    <div>
    <input type='text' id='userParagraph' size='20'>
    </div>

    <div id="par">

    <button id='heading'> Add your paragraph</button> 
    </div>
    </body>
    </html>

Here is Javascript code: 

window.onload = function() {
  document.getElementById("addheading").onclick = pCreate;
};

    function pCreate() {
      var userPar= document.createElement("p");
      var parNew = document.getElementById('userParagraph').value;
      userPar.innerHTML = par;
      var area = document.getElementById("par");
      area.appendChild(userPar);
}

3 Answers 3

1
userPar.innerHTML = par;

should be

userPar.innerHTML = parNew;
Sign up to request clarification or add additional context in comments.

1 Comment

There are more issues, for example document.getElementById("addheading").onclick should refer to heading.
1

In your code:

> window.onload = function() {  
>   document.getElementById("addheading").onclick = pCreate;
> };

Where it is possible (perhaps likely) that an element doesn't exist, best to check before calling methods:

    var addButton = document.getElementById("addheading");

    if (addButton) {
      addButton.onclick = pCreate;
    }

Also, there is no element with id "addheading", there is a button with id "heading" though.

> function pCreate() {
>   var userPar= document.createElement("p"); 
>   var parNew = document.getElementById('userParagraph').value;  
>   userPar.innerHTML = par;

I think you mean:

    userPar.innerHTML = parNew;

if you don't want users inserting random HTML into your page (perhaps you do), you can treat the input as text:

    userPar.appendChild(document.createTextNode(parNew));

.

>   var area = document.getElementById("par"); 
>   area.appendChild(userPar);
> }

Your variable names and element ids don't make a lot of sense, you might wish to name them after the data or function they represent.

Comments

0

I did it and it worked.


   <html lang='en'>
   <head>
   <title>Add Paragraph </title>
   <meta charset='utf-8' >
   <script>
    window.onload = function() {
  document.getElementById("heading").onclick = pCreate;
}

    function pCreate() {
      var userPar= document.createElement("p");
      var parNew = document.getElementById('userParagraph').value;
      userPar.innerHTML = parNew;
      var area = document.getElementById("par");
      area.appendChild(userPar);
}
   </script>
   </head>
   <body>
   <div>
   <input type='text' id='userParagraph' size='20'>
   </div>

   <div id="par">

   <button id='heading'> Add your paragraph</button>
   </div>
   </body>
   </html>```

1 Comment

Please, add brief explanation of what you did there. The goal here is to make people learn something new and not just copy paste your code.

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.