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

Asked

Viewed 189 times

-1

Chanted needing to take the contents of a div for my variable so I can treat it and send my email.

I’m trying with JS but is returning me error "Undefined", my code is like this:

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

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

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

2 answers

2

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

Example

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

There is also a similar alternative, which is the property textContent, but it is worth noting that there are significant differences between the two, according to informed in the documentation.

Example

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

References

  • Thank you for the reply, it has been solved

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, it is possible to put in a variable some external page using the library Curl

$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 access Urls, whereas the Curl library has for purpose a direct connection to a web page, whether internal to your domain, or even external. So for this reason I believe it is better to use the cUrl

Difference between file_get_contents and Curl

  • It worked here, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.