Skip to content
Advertisement

How to read the text in a input tag in a p tag?

HTML:

JavaScript

JS:

JavaScript

When I input some text into the input box the code still reads “placeholder…”, How do I make the code to speak the inputted text?

Advertisement

Answer

You are grabbing the innerHTML which isn’t going to read text, it’s going to read html.

In order to concatenate your input elements and your text, you’re actually going to need to combine the two somewhere in your code. Probably within the speak function.

The simplest way to do this is probably the following:

JavaScript

What the above does is iteratate over the child nodes of the inputTxt element. It grabs the textContent (plain text) of any text nodes or the value of any element nodes and stitches them together in order.

A Simple Example To See How This Works be sure to click the “compile” button below the input sentence

JavaScript
JavaScript

The following should work for you using your current code:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement