
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print div content using jQuery?
Let’s say we have the following button −
<input type='button' id='buttonId' value='Press Me To print the data'>
On click of the above button, call the function() with on() −
$('#buttonId').on('click', function () { displayTheData(); })
The above function is using the html() to display the following div −
<div id='printThisDivIdOnButtonClick'> <p>I am inside the div tag...</p> </div>
Example
Following is the complete code to print div content −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <body> <div id='printThisDivIdOnButtonClick'> <p>I am inside the div tag...</p> </div> <p>I am not inside the div tag...</p> <input type='button' id='buttonId' value='Press Me To print the data'> <div id="printTheDivisionValue"></div> </body> <script> $('#buttonId').on('click', function () { displayTheData(); }) function displayTheData() { $(document).ready(function () { $("#printTheDivisionValue").html($("#printThisDivIdOnButtonClick").html()); }); } </script> </html>
To run the above program, save the file name anyName.html (index.html). Right click on the file and select the option “Open with Live Server” in VS Code editor.
Output
The output is as follows −
Now click the button, you will get the following output.
The output is as follows −
- Related Questions & Answers
- How to get the value of div content using jQuery?
- How to duplicate a div using jQuery?
- How to create div dynamically using jQuery?
- How to toggle a div visibility using jQuery?
- How to wrap tables with div element using jQuery?
- How to load a page in div using jQuery?
- How to replace innerHTML of a div using jQuery?
- How to disable copy content function using jQuery?
- Slide up and down of a div animates its content - jQuery?
- How to scroll to element in horizontal div using jQuery?
- How to replace innerHTML of a div tag using jQuery?
- How to get innerHTML of a div tag using jQuery?
- How to wrap first few items in div using jQuery?
- How to replace only text inside a div using jQuery?
- How to check if a div is visible using jQuery?
Advertisements