1

Having something like this:

<div id="myDiv"><script>document.write('SOMETHING');</script></div>

Now i want to get "SOMETHING". when i try

$("#myDiv").text();

it returns:

<script>document.write('SOMETHING');</script>

Thanks

freedomn-m
  • 27,664
  • 8
  • 35
  • 57

1 Answers1

2

You can use innerText property. ( textContent will not work as expected as it will log all content of the script. )

const div = document.getElementById('myDiv')

console.log(div.innerText)
<div id="myDiv"><script>document.write('SOMETHING');</script></div>
Mina
  • 14,386
  • 3
  • 13
  • 26
  • and if you want to stick with jQuery since the question was revolving around that just grab the HTMLElement doing: `$("#myDiv")[0].innerText` – Diego D Jul 20 '22 at 10:16
  • Just out of curiosity, OP is using JQuery and I don't remember if `innerText` is proxied. If OP needs it in JQuery maybe it would worth including it in the answer – Christian Vincenzo Traina Jul 20 '22 at 10:16
  • Diego answered it :) thanks – Christian Vincenzo Traina Jul 20 '22 at 10:16
  • Your code here, and in my script works perfect. thanks. But my own code, even when i replace it with yours, will fail. have no idea why. ps: i made a loop to read table cells and the content i have problem with is some of tds. tried javascript and jquery. both failed.. – Behzad Safamanesh Jul 20 '22 at 11:20
  • Make sure your code runs after the doc.write code - move your ` – freedomn-m Jul 20 '22 at 12:12