I have the following HTML
<div id="example">
...some text...
<script type="text/javascript">
... some javascript...
</script>
</div>
How to get content of #example
but also with the JavaScript?
$("#example").html(),
$("#example").text(),
$("#example").val()
all don't work.
Use the textContent property to get the text of a div element, e.g. const result = element. textContent . The textContent property will return the text content of the div and its descendants. If the element is empty, an empty string is returned.
You can add <script></script> inside a DIV tag. Just check on w3c, it is valid HTML. Save this answer.
To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.
The html()
method should work for you. Are you sure you are running the code after the DOM is completed?
$(document).ready(function(){
alert($("#example").html());
});
Working Demo
You can use
html(): Get the html contents (innerHTML) of the first matched element.
var contents = $("#example").html();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With