Home > Software engineering >  Copying text from the page and placing it in a input field when a button is clicked with PHP
Copying text from the page and placing it in a input field when a button is clicked with PHP

Time:07-28

I'm looking to copy the text from a <p> element with the ID of code and put it in an input field with the data-field-id of 6197c68b4c806 when a button is clicked.

How could I do this? I would like the code to be inline if that is possible.

CodePudding user response:

First you need to access the element you want to copy the text from with:

`let text = document.querySelector('css selector').text` or .textContent.

This will save the text existing in the element. Then you select the input, using the same technique and then do: .value = text. Also add an event listener to the button you want to click to trigger this:

document.querySelector('btn.submit').addEventListener(() => {
let text = document.querySelector('css selector').text;
document.querySelector('input#x').value = text
})

Should do the trick.

CodePudding user response:

       document.queryselector('#btn').addEventListener(()=>
var q_t = document.queryselector('#input').text;
document.queryselector('#output').text(q_t);
    )}

You have to follow this above code, hopefully you can solve this

  • Related