JavaScript - Display Random Character Name

Submitted by: 
Language: 
Visitors have accessed this post 102 times.

In this tutorial we will create a Display Random Character Name using JavaScript. This code will generate a random character name when the user click the button. The code itself use a onclick() to call a specific method that can generate a random character name using Math.floor() and passing an argument Math.random() multiply by an array length to display a random array index value. You can apply this script to your code, it is a user-friendly program feel free to modify it.

We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.
.

Getting Started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Display Random Character Name</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3">
  18. <center><button class="btn btn-primary" onclick="generateCharacter()">Generate Character</button></center>
  19. <br />
  20. <center><a href="index.html" class="btn btn-success">Reset</a></center>
  21. </div>
  22. <div class="col-md-6">
  23. <div id="result"></div>
  24. </div>
  25. </div>
  26. <script src="js/script.js"></script>
  27. </body>
  28. </html>

Creating the Script

This code contains the script of the application. This code will generate a random character name when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. var count=1;
  2. function generateCharacter(){
  3. var html;
  4. var firstname = ["Tony", "Bruce", "Peter", "Steve", "Reed"];
  5. var lastname= ["Stark", "Banner", "Parker", "Roger", "Richards"];
  6. var first_random = Math.floor(Math.random()*firstname.length);
  7. var last_random = Math.floor(Math.random()*lastname.length);
  8. html="<h5>Character "+count+"</h5>"
  9. +"<h3>"+firstname[first_random]+" "+lastname[last_random]+"</h3>"
  10. +"<hr style='border-top:1px solid #000;'/>";
  11. document.getElementById('result').innerHTML+=html;
  12.  
  13. count++;
  14.  
  15.  
  16. }

There you have it we successfully created a Display Random Character Name using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.