www.webdeveloper.com
Results 1 to 4 of 4

Thread: Need Help!!! functions

  1. #1
    Join Date
    May 2013
    Posts
    2

    Need Help!!! functions

    I'm trying to get the album names to pop up in alerts. I'm not sure if my problem is the calling of the function in the HTML code or it's the JavaScript code itself.
    Here's what i got:
    HTML:

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Store</title>
    <script src="store.js" type="text/javascript"></script>
    <script src="cart.js" type="text/javascript"></script>
    </head>
    <body>
    <h1>Titan Music Store</h1><br>
    <a href="index.html">Home</a><br><br>
    <form><input type="submit" value="Get Albums" onClick="getAlbums()" id="albums"></form>
    </body>

    JavaScript:

    var album1 {
    title:'To the Moon';
    artist:'Bruno Mars';
    price:'$12.99';
    release Date:'1/17/13';
    quantity:'4';
    var tracks1 = ['Moonshine', 'Natalie', 'Treasure', 'Way You Are'];
    }
    var album2 {
    title:'In Love';
    artist:'Robin Thicke';
    price:'$12.99';
    release Date:'2/24/13';
    quantity:'4';
    var tracks2 = ['All the Way', 'Lost Without You', 'Tonight', 'Can You Believe'];
    }
    var album3 {
    title:'Karma';
    artist:'Alicia Keys';
    price:'$12.99';
    release Date:'4/7/13';
    quantity:'4';
    var tracks3 = ['NoOne', 'Fallen', 'Womens Worth','Loved' ];
    }

    function getAlbums(){
    //List all albums

    alert (album1.title);
    alert (album2.title);
    alert (album3.title);
    }

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by Novice82 View Post
    I'm trying to get the album names to pop up in alerts. I'm not sure if my problem is the calling of the function in the HTML code or it's the JavaScript code itself.
    Somewhere in your browser's menu system you will have seen the option 'Error Console'. You need to use it.

  3. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    hints:

    • var album1 {
    • the Moon';
    • var tracks1 = [
    • release Date:


    plus doing what Logic Ali suggested (not only this time, but always)

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,674
    it would be better to use instances of one previously created object instead of creating new objects every time. try this:
    Code:
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Store</title>
    <script type="text/javascript">
    function album(title,artist,price,releaseDate,quantity,tracks){
    this.title=title;
    this.artist=artist;
    this.price=price;
    this.releaseDate=releaseDate;
    this.quantity=quantity;
    this.tracks=tracks;
    }
     
    var album1=new album('To the Moon','Bruno Mars','$12.99','1/17/13','4',[' Moonshine',' Natalie',' Treasure',' Way You Are']),
    album2=new album('In Love','Robin Thicke','$12.99','2/24/13','10',[' All the Way',' Lost Without You',' Tonight',' Can You Believe']),
    album3=new album('Karma','Alicia Keys','$12.99','4/7/13','47',[' NoOne',' Fallen',' Womens Worth',' Loved']);
     
    function getAlbums(){
    var i=1,o;
    while(o = eval('window.album'+i)){
    document.getElementById('output').innerHTML+=' <b>'+o.artist+'</b>, "'+o.title+'" ('+o.tracks+' ) | release '+o.releaseDate+' -> '+o.price+' q-ty ['+o.quantity+']<br />';
    i++;
    }
    }
     
    window.onload=getAlbums;
    </script>
    </head>
    <body>
    <h1>Titan Music Store</h1><br>
    <div id="output"></div>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles