Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get content of div which contains JavaScript script blocks?
To get the content of a div which contains JavaScript script blocks, use the jQuery html() method. This method retrieves the HTML content inside the selected element, including any script tags and their content. The html() method is particularly useful when you need to access or manipulate div content that includes embedded JavaScript code.
Example
The following example demonstrates how to use the html() method to retrieve the complete content of a div element that contains both text and JavaScript script blocks ?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button1").click(function(){
alert($("#demo").html());
});
});
</script>
</head>
<body>
<div id="demo">
This is demo text.
<script>
// This is JavaScript content
var message = "Hello World";
</script>
</div>
<button id="button1">Get Content</button>
</body>
</html>
The output of the above code when the button is clicked will display an alert containing ?
This is demo text.
<script>
// This is JavaScript content
var message = "Hello World";
</script>
Conclusion
The jQuery html() method effectively retrieves the complete HTML content of a div element, including any JavaScript script blocks within it, making it ideal for scenarios where you need to access or process div content that contains embedded scripts.
