Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JavaScript array get elements at random 
 

In this example of getting JavaScript array elements randomly we have printed the array elements by the random index position of the array.

 

JavaScript array get elements at random

                         

In this example of getting JavaScript array elements randomly we have printed the array elements by the random index position of the array. For generating random numbers we have used the Math.random() function.

In our HTML code we have created an array of ten elements and have stored ten elements in this array object arrName. Now in the variables  firstRandomNumber, secondRandomNumber and thirdRandomNumber we have generated three random numbers between value "0" and "9". Each and every time when the page is refreshed it changes the value of these three random variable and therefore we get the three different elements each time in the running page. Here is the full example code for printing the array elements by the random index position: 

 

 

 

javascript_array_random.html

<html>
  <head>
    <title> JavaScript array random </title>
    <script type="text/javascript">
      var arrName = new Array(10);
      arrName[0"Amit";
      arrName[1"Santosh";
      arrName[2"Rose";
      arrName[3"India";
      arrName[4"News";
      arrName[5"Track";
      arrName[6"Sandeep";
      arrName[7"Suman";
      arrName[8"Saurabh";
      arrName[9"Vineet";
        var firstRandomNumber = (Math.round((Math.random()*8)+1))
      document.write("<b>First element:=</b>"
        +arrName
[firstRandomNumber]+"</br>");
        var secondRandomNumber = (Math.round((Math.random()*8)+1))
      document.write("<b>Second element:=</b>"
        +arrName
[secondRandomNumber]+"</br>");
        var thirdRandomNumber = (Math.round((Math.random()*8)+1))
      document.write("<b>Third element:=</b>"
        +arrName
[thirdRandomNumber]+"</br>");
    </script>
  </head>
  <body bgcolor="#ffffdd">
  <form>
    <h2><font color="blue">JavaScript array Random Example</font></h2>
    <input type="submit" value="Refresh"/>
  </form>
  </body>
</html>

Output is as follows:

It shows three random elements from the array at three position in the HTML page.

If you click on the "Refresh" button it will print different array elements randomly.

Download Sample Source Code

                         

» View all related tutorials
Related Tags: java javascript c string array join script object method ip using int element elements to ld generate e can put

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

how can i get the elementy from servlet page to jsp using weblogic server

Posted by bala on Wednesday, 12.3.08 @ 01:37am | #82306

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.