Home > Software design >  Is it possible to pass value of <p> to <input>
Is it possible to pass value of <p> to <input>

Time:10-01

I have a <p> that prints a value is it possible that the value can be printed in the <input>?

for example:

<p>2</p>

<input type="number">2</input>

CodePudding user response:

This will require the use of Javascript. Add an id to your paragraph and input so you can identify them, then set the value of your input field to the textContent of your paragraph:

let myParagraph = document.getElementById('my-paragraph');
let myInput = document.getElementById('my-input');

myInput.value = myParagraph.textContent;
<p id="my-paragraph">2</p>

<input type="number" id="my-input"/>

CodePudding user response:

Input element has an attribute called value.
So you can do -

<input type="number" value=2 />
  • Related