All TalkersCode Topics

Follow TalkersCode On Social Media

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

For Loop In jQuery

Last Updated : Mar 11, 2024

For Loop In jQuery

In this article we will show you the solution of for loop in jQuery, here we are going to show you example with for loop to print numbers up to 10 on webpage with some string by using p tag id ‘res’.

As we know for loop is used to iterate some process until it reaches condition true so we can reduce time and space to avoids repeat of some code in same program.

Step By Step Guide On For Loop In jQuery :-

Here we defined button ‘Click’ with id attribute for generates process and p element defined with id attribute ‘res’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and we waiting for click() method’s click event occurrence which is added on button.

Within this we needs to define for loop with one iteration variable, using loop we printing numbers 1 to 10 on webpage.

<!DOCTYPE html>
<html>
    <head>
        <title>FOR LOOP</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                    $("button").click(function(){
                        for(i=1;i<=10;i++){
                            $("#res").append("The Number Is "+i+"<br>");
                        }
                    });
                })
        </script>
        </head>
        <body>
            <button id="btn">Click</button>
            <p id="res"></p>
        </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 added click method with button element by tag name ‘button’.
  8. Within function we defined for loop, here variable ‘i’ used for does iteration and we sets i initial value to ‘1’, condition sets to up to value ‘10’, i++ refers increment by 1 for each iteration.
  9. In this loop we appending ‘The Number Is’ with variable i on webpage p tag by specifying id ‘res’ for displaying on webpage.
  10. In html block we defined button ‘Click’ with id attribute ‘btn’ and p tag with id ‘res’.
  11. 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 use for loop in 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 ‘click’ button, now user needs to clicks on it then it will shows common text ‘The Number Is’ with numbers 1 to 10.

I hope this article on for loop in jQuery helps you and the steps and method mentioned above are 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 🡪