PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

[FIXED] How to update an empty paragraph with userinput using pure javascript?

 October 16, 2022     dom, dom-events, javascript     No comments   

Issue

This example in jQuery is what I am looking for in pure JavaScript.

It's an example of a <textarea> where a paragraph <p></p> is updated as the user types in the textarea.

My question is how can it be done in pure JavaScript?

An attempt on my behalf looks like this but it doesn't quite work:

HTML:

<div class="textarea">
    <textarea name="myText" id="myText" rows="2"></textarea>
</div>
<div>
    <p id="textOuput"></p>
</div>

JavaScript:

var textArea = document.getElementById('myText');

textArea.onkeyup = function() {
    var val = textArea.value,
    textOutput = document.getElementById('textOutput');
    textOutput.innerHTML = val; // Uncaught TypeError: Cannot set property 'innerHTML' of null
};

Solution

You have a typo in your id of the paragraph:

<p id="textOuput"></p>

should be:

<p id="textOutput"></p>

Adding Utkanos' comments, the JS code could look like this:

var textArea = document.getElementById('myText');
var textOutput = document.getElementById('textOutput');
textArea.onkeyup = function() {
    textOutput.innerHTML = textArea.value;
};


Answered By - Tibos
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing