Source Code for the showBook() Example : Objects Object Oriented « Language Basics « JavaScript DHTML






Source Code for the showBook() Example

 

<html>
<head>
  <title>Using the book object</title>
  <script type="text/javascript">
  <!--
  function book(title, author, ISBN, subject,  rating) {
    this.title = title;
    this.author = author;
    this.ISBN = ISBN;
    this.subject = subject;
    this.rating = rating;
    this.show = show;
  }
   
  function show() {    
    alert(this.title + " "+ this.author + " " + this.ISBN + " " + this.subject + " " + this.rating);
  }
   
  function assignRating() {
    selectedBook = document.form1.bookList.options[document.form1.bookList.selectedIndex].value;
    selectedBook = eval(selectedBook);
    selectedBook.rating = document.form1.rating.options[document.form1.rating.selectedIndex].text;
  }
   
  function showBook() {
    selectedBook = document.form1.bookList.options[document.form1.bookList.selectedIndex].value;
    selectedBook = eval(selectedBook);
    selectedBook.show();
  }
  // Execute on loading
  dbBook = new book("A", "AA","1-11111-118-1", "A1", 5);
  fkBook = new book("B", "BB","1-22222-112-1", "B1", 5);
  olBook = new book("C", "CC","1-33333-118-1", "C1", 4);
  iaBook = new book("D", "DD","1-44444-118-1", "D1", 3);
  cnBook = new book("E", "EE","1-55555-128-1", "F1", 5);
   
  //-->
  </script>
</head><body>
  <h1>Book Objects</h1>
  <form name="form1">
    <p>
      Select a book: 
    </p>
    <p>
      <select name="bookList" size=1>
        <option value="dbBook">A</option>
        <option value="fkBook">B</option>
        <option value="olBook">C</option>
        <option value="iaBook">D</option>
        <option value="cnBook">E</option>
      </select>
    </p>
    <p>
      Assign a rating: 
    </p>
    <p>
      <select name="rating" size=1>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select> 
    </p>
    <p>
      Click to assign: 
    </p>
    <p>
      <input type="button" name="Assign" value="Assign" onClick="assignRating()">
    </p>
    <p>
      Click to show: 
    </p>
    <p>
      <input type="button" name="Show" value="Show" onClick="showBook()">
    </p>
  </form></body>
   
</html>


           
         
  








Related examples in the same category

1.Object utility: create, parse and profile
2.Define Object, use its instance
3.Define and use object
4. Creating an Object and Using Object Instance Properties and Methods
5. Object-Oriented Planetary Data Presentation
6.The Book Object Definition
7.Complete Example of Using the employee, client, and project Objects
8.Using the Book Object Constructor
9.Creating Objects Dynamically
10.Object to array
11.Utility class for JavaScript class definition
12.Create an object and add attributes
13.Use for in loop to display all attributes from an object
14.Display the properties from an object one by one
15.An object and its constructor
16.Use function as the object constructor