2

I would like to pass some data to a python code with javascript. I used the onclick function of a button configuring it like this:

const button = document.getElementById('btn-id');
button.addEventListener('click', async _ => {
    try {
      console.log(document.querySelector('.area-id'))
      const response = await fetch('/', {
        method: 'post',
        body: document.querySelector('.area-id').innerText,
      });
      console.log('Completed!', response);
    } catch(err) {
      console.error(`ERRORE!`);
      console.error(`Error: ${err}`);
    }
});

However when I try to print the text of the textarea it comes out as an empty string... could someone help me?

1 Answer 1

2

The correct way to obtain the text added to a textarea is to use the .value property, not the .innerText

So simply change to

body: document.querySelector('.area-id').value

And your fetch request should be correct. I am assuming of course that you have the correct class "area-id" in your textarea

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.