All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Add Text To DIV

Last Updated : Mar 11, 2024

jQuery Add Text To DIV

In this article we will show you the solution of jQuery add text to div, here we needs to use click() and text() method for achieve the result.

As we know the click() triggers click event and which is added on selected clickable element with the help of we proceeds some process.

The text() method is used for appends text on selected element in html document.

Step By Step Guide On jQuery Add Text To DIV :-

In html we defined div element and button ‘Add Text’. In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser.

There we appended click() method on button and within this we specified div element then which is appends with text() method for sets some text on div tag.

<!DOCTYPE html>
<html>
    <head>
        <title>Add text to div</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $("div").text("This is div element or we can say block element");
                })
            });
        </script>
    <body>
        <div></div>
        <button>Add Text</button>
    </body>
    </head>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. We defined div element for inserts text on it and button ‘Add Text’ for proceed process when user clicks on it.
  8. In script we defined ready() method within that we appended click() method on button ‘Add Text’ by specifying tag name ‘button’. There we specified div element by tag name ‘div’ and which is appends with text() method with string ‘This is div element or we can say block element’.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to add text on div element using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see button ‘Add Text’ now user needs to clicks on it then we can see added text ‘This is div element or we can say block element’ on div element.

I hope this article on jQuery add text to div helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪