Create a Random User Generator using jQuery Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report With the use of the API and jQuery, we'll create a random user generator app. A straightforward web application called "jQuery Random User Generator" makes use of jQuery and the RandomUser.me API to generate random user data and present it in a visually appealing way. Users of this project can click a button to get random user information, such as name, gender, location, phone number, email, and profile image.PrerequisitesHTMLCSSjQueryApproachWhen the user clicks on the "Random Person" button, it automatically becomes inactive and displays the text "Loading...". to provide feedback to the user about the ongoing process.The 'getPerson' function sends an AJAX request to the RandomUser.me API to fetch random user data.Upon receiving the data, the '.done' callback processes the information. Subsequently, the 'showData' function gracefully updates the DOM elements by incorporating the new user's details.In case of an error, the '.fail' callback displays an alert message to notify the user of the failure.The code guarantees the reactivation of the "Random Person" button and restores its initial text state enabling users to click it again to fetch another random user.Example: Below is the implementation of the project. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE" /> <meta name="viewport" content= "width=device-width, initial-scale=1.0" /> <title>Random User Generator using jQuery</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="wrapper"> <div class="img-area"> <div class="inner-area"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/20230816223829/geeksgforgeeks-logo-1.png" id="photo" /> </div> </div> <div id="name">John Doe</div> <hr class="horizon" /> <div class="info"> <p>First Name : <span id="first">John</span></p> <p>Last Name : <span id="last">Doe</span></p> <p>Gender : <span id="gender">Male</span></p> <p>Location : <span id="street">Earth</span></p> <p>Phone : <span id="phone">333-333-2222</span></p> <p>Email : <span id="email">[email protected]</span></p> </div> <button id="btn">Random Person</button> </div> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script> <script src="script.js"></script> </body> </html> CSS body { font-family: Arial, sans-serif; background-color: #f2f2f2; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; } .wrapper { background-color: #fff; border-radius: 15px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.2); text-align: center; padding: 20px; width: 400px; margin: 20px; transition: transform 0.2s; } .wrapper:hover { transform: scale(1.02); } .img-area { margin: 20px 0; } #photo { width: 150px; height: 150px; border-radius: 50%; box-shadow: 0px 1px 20px 1px grey; } #name { font-size: 24px; font-weight: bold; margin: 20px 0; } .horizon { margin: 20px 0; } .info { text-align: center; } p { margin: 0; font-size: 18px; padding: 10px; } button { background-color: #007BFF; color: #fff; border: none; border-radius: 5px; padding: 10px 20px; font-size: 16px; cursor: pointer; transition: background-color 0.3s; margin-top: 20px; } button:hover { background-color: #0056b3; } @media (max-width: 480px) { .wrapper { padding: 10px; } #photo { width: 100px; height: 100px; } #name { font-size: 20px; } p { font-size: 16px; } button { padding: 8px 16px; font-size: 14px; } } JavaScript $(document).ready(function () { $("#btn").click(function () { $("#btn").prop("disabled", true); $("#btn").text("Loading..."); getPerson(); }); function getPerson() { const url = "https://.../api/"; $.get(url) .done(function (data) { const person = data.results[0]; showData(person); }) .fail(function () { // Handle error here alert("Failed to fetch data. Please try again."); }) .always(function () { $("#btn").prop("disabled", false); $("#btn").text("Random Person"); }); } function showData(person) { $("#name").text(`${person.name.first} ${person.name.last}`); $("#first").text(person.name.first); $("#last").text(person.name.last); $("#street").text(person.location.street.name); $("#phone").text(person.phone); $("#email").text(person.email); $("#gender").text(person.gender); $("#photo").attr("src", person.picture.large); } }); Output: Comment More infoAdvertise with us Next Article Build a Random Name Generator using ReactJS S saurabhkumarsharma05 Follow Improve Article Tags : HTML Web Development Projects Geeks Premier League 2023 jQuery-Projects Similar Reads Create a Password Generator using HTML CSS and jQuery In this article, we will create a password generator using jQuery. With this tool, users can easily customize their password by specifying the desired length and selecting options like including symbols, numbers, lowercase, and uppeÂrcase letters. Furthermore, the geneÂrated password can be conveni 3 min read Build a Random User Generator App Using ReactJS In this article, we will create a random user generator application using API and React JS. A Random User Generator App Using React Js is a web application built with the React.js library that generates random user profiles. It typically retrieves and displays details like names, photos, and contact 4 min read Build a Random Name Generator using ReactJS In this article, a Random Name GeÂnerator will be created using React.js. Building a Random Name Generator means creating a program or application that generates random names, typically for various purposes like usernames, fictional characters, or data testing. It usually involves combining or selec 4 min read QR Code Generator using HTML, CSS and jQuery In this article, we will learn how to build a QR generator using HTML, CSS, and jQuery. A QR code generator is an application that stores any required textual data into a QR code which can be later scanned with a QR code scanner to reveal the stored information. This code generator consists of an in 3 min read Create an QR Code Generator Project using HTML CSS & JavaScript Today, more than 75% of websites and apps use QR codes to share links, contact info, or event details quickly. Here, youâll learn how to make your own QR Code Generator using HTML, CSS, and JavaScript. Weâll guide you step by step, so even if youâre a beginner, you can follow along easily. By the en 3 min read Number Sequences Generator using HTML CSS and JavaScript In this article, we are going to implement a number sequence generator based on HTML, CSS, and JavaScript. In this program, we have three different types of sequences: the Fibonacci Series, the Prime Number Series, and the Even Odd Number Series. Preview of final output: Let us have a look at how th 5 min read Like