What is the best way to get content inside a div for a variable?

-1

I needed to get the contents of a div to my variable so that I can handle it and send it as an email.

I'm trying with JS however it's returning me error "undefined", my code looks like this:

var pegarTxt = document.getElementById("Total").text;
document.getElementById('TotalFinaliz').innerHTML = pegarTxt;

Where I'm wrong, because apparently the logic is correct, remembering where I'm trying to display is invisible and only shows after everything is correct.

I would also like to know if it is possible to pull the contents of the div via php without having to use file_get_contents, as I think this would slow it down.

    
asked by anonymous 22.06.2018 / 07:15

2 answers

1

Adding content to an input field before sending it

    var pegarTxt = document.getElementById("Total").innerText;
    //para textarea
    document.getElementById('TotalFinaliz').value = pegarTxt;
    document.getElementById('TotalFinaliz2').innerText = pegarTxt;
    document.getElementById('TotalFinaliz3').innerHTML = pegarTxt;

    var pegarTxt = document.querySelector('div').textContent;
    document.getElementById('TotalFinaliz4').innerHTML = pegarTxt;

    //para input somente .value
    document.getElementById('TotalFinaliz5').value = pegarTxt;
    <div id="Total">Entou necessitando pegar o conteudo de uma div para minha varivel para que eu possa tratar ela e fazer meu envio de email</div>

    <textarea id="TotalFinaliz"></textarea>
    
    <textarea id="TotalFinaliz2"></textarea>
    
    <textarea id="TotalFinaliz3"></textarea>

    <textarea id="TotalFinaliz4"></textarea>

    <input id="TotalFinaliz5">

var pegarTxt = document.getElementById("Total").innerText;

//no textarea de indice 2
document.getElementsByClassName("RonaldoM")[2].innerText = pegarTxt;
<textarea class="RonaldoM"></textarea>
<textarea class="RonaldoM"></textarea>
<textarea class="RonaldoM"></textarea>

<div id="Total">Entou necessitando pegar o conteudo de uma div para minha varivel para que eu possa tratar ela e fazer meu envio de email</div>
  

There are many servers that do not allow the file_get_contents () function to be used. For these cases, you can put some external page in a variable using the cURL library.

$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, 'http://exemplo.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$conteudo = curl_exec ($ch);
curl_close($ch);

O conteúdo da página  estará na variável $conteudo.
  

file_get_contents has the purpose of reading files, and by chance also accessing Urls, since the cURL library is intended for a direct connection to a web page, whether it is internal to your domain, or even external. So for this reason I believe it's best to use cUrl

Difference between file_get_contents and curl

    
22.06.2018 / 08:29
2

The correct way to retrieve "rendered" content is by using the innerText

Example

var p = document.querySelector('p').innerText;
console.log(p);
<p>Stackoverflow Português</p>

There is also a similar alternative, which is textContent property . , but it's worth noting that there are significant differences between the two, as per in the documentation.

Example

var p = document.querySelector('p').textContent;
console.log(p);
<p>Stackoverflow Português</p>

References

22.06.2018 / 08:25