How can I add a div
content to a input
using js
I have this code
<div>This is text</div>
<input type="text" name="test" id="inputs">
<button>Go</button>
CodePudding user response:
document.querySelector('button').addEventListener('click', evt => {
let str = document.querySelector('#inputs').value;
document.querySelector('div').innerHTML = '<div>' str '</div';
});
<div>This is text</div>
<input type="text" name="test" id="inputs">
<button>Go</button>
CodePudding user response:
I'm assuming you need to change the value of input to div's content on the button's click.
const main = () => {
// declare the variables
const div = document.querySelector('div')
const input = document.querySelector('input')
const button = document.querySelector('button')
// defining the click listener
const handleButtonClick = () => {
const divContent = div.innerText
input.value = divContent
}
// attaching the `click` listener to button
button.addEventListener('click', handleButtonClick)
}
window.addEventListener('DOMContentLoaded', main)
<div>This is text</div>
<input type="text" name="test" id="inputs">
<button>Go</button>