All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Create Element

Last Updated : Mar 11, 2024

jQuery Create Element

In this article we will show you the solution of jQuery create element. here we needs to use appendTo() and click() methods.

The appendTo() is an inbuilt method in jQuery that is used to insert HTML element at the end of the selected element and as we know click() method used for loads some process when user clicks on button element defined html code. So here we can create element easily using jquery.

Step By Step Guide On jQuery Create Element :-

Here we defined button ‘Create Element’ for generate process of creating h1 element when user clicks on it.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with button then there we specifying h1 element of start and end tags with some text then we appended on body element.

<!DOCTYPE html>
<html>
    <head>
        <title>CREATE ELEMENT</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $('button').click(function(){
                    $('<h1>Welcome All</h1>').appendTo('body');
                })
            })
        </script>
        </head>
        <body>
            <button>Create Element</button>
        </body>
</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. In script we defined ready() method within that we appending click() method with button by using tag name ‘button’ for generate click event when user clicks on ‘Create Element’ button.
  8. Within function we needs to specify h1 tags with string ‘Welcome All’ then which is appended on body element by appendTo() method. In html block we defined button ‘Create Element’ for loads script code.
  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 create 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 ‘Create Element’ now user needs to click on it then we can see created h1 element of text ‘Welcome All’ below button.

If you want to create any other html element then modify above defined code accordingly.

I hope this article on jQuery create element helps you and the steps and method mentioned above ar easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪