0

I'm looking to dynamically add content to my page using jQuery and JavaScript, but having issues loading the content. I've created 4 separate objects, and I'm using a function to add each object's content to the page when I invoke it with each argument (book1, book2, album1, album2). I would like to be able to have the page load the objects and respective content to the page as such: book1, book2, album1, album2.

At the moment, however, I'm seeing the "name", "category", and "price" property values from book2 (Life of Pi), along with the "selling_points" values from book1 on each of the 4 divs when I load the page, instead.

*I'm currently getting an "Uncaught TypeError: Cannot read property 'forEach' of undefined" for the "product.selling_points.forEach(function(point)" part of my code when I check the console.

Also, I am not sure how to write the code to add each object's respective picture_url; I'm currently simply directly adding the img url I'd like to use for book1 to each div.

body {
  background-color: green;
}
#header {
  background-color: red;
  font-size:300%;
  text-align: center;
  color: purple;
  font-weight: bold;
}
#navbar {
  background-color:blue;
  text-align: center;
}
#content {
  background-color: red;
  text-align: center;
  font-size:150%;
  font-style: oblique;
  color:darkblue;
}
#book1 {
  background-color: red;
  font: black;
}
#book2 {
  background-color: red;
  font: black;
}
#album1 {
  background-color: red;
  font: black;
}
#album2 {
  background-color: red;
  font: black;
}
.image {
  height:600px;
  width:420px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}
p {
  font-size:120%;
  text-align: center;
}
.name {
  font-size: 150%;
}
  background-color: red;
  font: black;
}
#album1 {
  background-color: red;
  font: black;
}
#album2 {
  background-color: red;
  font: black;
}
.image {
  height:600px;
  width:420px;

}
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <title>Blamazon</title>
</head>
<body>
<div id="header">BLAMAZON</div><br>
<div id="content">Products</div><br>
  <div id="book1">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
  <div id="book2">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
  <div id="album1">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>
  <div id="album2">
    <p class="name"></p>
    <p class="category"></p>
    <p class="price"></p>
    <img class="image">
    <p class="description"></p>
  </div>

<div id="footer"></div>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
<script>
var book1, book2, album1, album2

book1 = {
    product_id: 'book1',
    "name": "Thinking Fast and Slow",
    "category": "Non-Fiction",
    "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
    "price": "$7.07",
    "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"]
}

book2 = {
    product_id: 'book2',
    "name": "The Life of Pi",
    "category": "Fiction",
    "picture-url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
    "price": "$9.07",
    "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"]
}

album1 = {
    product_id: 'album1',
    "name": "Back in Black, AC DC",
    "category": "Hard Rock",
    "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
    "price": "$12.07",
    "selling_points": ["Oldie but a goodie", "Will help you rock out"]
}

album2 = {
    product_id: 'album2',
    "name": "Good kid maad city",
    "category": "Hip-Hop",
    "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
    "price": "$12.07",
    "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"]
}

var add_product = function(product) {
    var $prod = $('#' + product.product_id)
    $prod.find('.name').text(product.name)
    $prod.find('.category').text(product.category)
    $prod.find('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $prod.find('.price').text(product.price)
    product.selling_points.forEach(function(point) {
        $prod.find('.description').append("<div><br>" + point + "</div><br>")
    })
}

add_product(book1)
add_product(book2)
add_product(album1)
add_product(album2)

/*
  var add_product = function(product) {
    $('.name').text(product.name)
    $('.category').text(product.category)
    $('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $('.price').text(product.price)
    product.selling_points.forEach(function(point) {
      $('.description').append("<div><br>" + point + "</div><br>")
    })
  }

  add_product(book1)
  add_product(book2)
  add_product(album1)
  add_product(album2)
*/
</script>
</body>
</html>

0

3 Answers 3

3

Actually you just have a typo in album1 - you have selling-points instead of selling_points. I think your script loading is ok. The reason you are having description, etc. in incorrect spots is the $(.classname) finds all elements in the page with the class name. Try adding a qualifier by adding a product_id, and finding the class name relative to the qualifier - like this:

var book1, book2, album1, album2

book1 = {
    product_id: 'book1',
    "name": "Thinking Fast and Slow",
    "category": "Non-Fiction",
    "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
    "price": 7.07,
    "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"]
}

book2 = {
    product_id: 'book2',
    "name": "The Life of Pi",
    "category": "Fiction",
    "picture-url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
    "price": 9.07,
    "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"]
}

album1 = {
    product_id: 'album1',
    "name": "Back in Black, AC DC",
    "category": "Hard Rock",
    "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
    "price": 12.07,
    "selling_points": ["Oldie but a goodie", "Will help you rock out"]
}

album2 = {
    product_id: 'album2',
    "name": "Good kid maad city",
    "category": "Hip-Hop",
    "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
    "price": 12.07,
    "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"]
}

var add_product = function(product) {
    var $prod = $('#' + product.product_id)
    $prod.find('.name').text(product.name)
    $prod.find('.category').text(product.category)
    $prod.find('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $prod.find('.price').text(product.price)
    product.selling_points.forEach(function(point) {
        $prod.find('.description').append("<div><br>" + point + "</div><br>")
    })
}

add_product(book1)
add_product(book2)
add_product(album1)
add_product(album2)
Sign up to request clarification or add additional context in comments.

5 Comments

Thx @nril, I did edit that but I'm still having the exact same issues. I'm only seeing book2's "name", "category", and "price" values/content, along with book1's "selling-points"in each of the 4 divs when I load the page. Also, I don't know how to dynamically add each individual picture_url to the page properly...
I'm using your suggestion, but when I load the page now I don't see any content at all. I also am still getting the error on my console: "Uncaught TypeError: Cannot read property 'forEach' of undefined"
@learningnow - if you could re-add your code again I'll take another look. I tested this on Firefox and it worked..
that works great now, not sure what was happening before (I think my html was still dirty at the time). Do you know how I can make sure that the function places a separate url image for each product (as defined in the object)? Right now I'm using .attr to place in the image I want for book1, but that means the other 3 divs use the same image also of course.
@learningnow - you can just refer to the property in your loop instead of: .attr('src','ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg') use: .attr('src',product.picture_url)
1

You were trying to load in the jQuery file within a .js file. You also had <script></script> tags in your file. These are not required and may break the file being read.

Instead, load the JS within your HTML.

If you really need to load jQuery into the JS file, i would recommend using this code:

var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-2.1.1.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

But all this really does is append the code to your HTML anyway.

  var book1, book2, album1, album2

  book1 = {
    "name": "Thinking Fast and Slow",
    "category": "Non-Fiction",
    "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
    "price": 7.07,
    "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"]
  }

  book2 = {
    "name": "The Life of Pi",
    "category": "Fiction",
    "picture-url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
    "price": 9.07,
    "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"]
  }

  album1 = {
    "name": "Back in Black, AC DC",
    "category": "Hard Rock",
    "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
    "price": 12.07,
    "selling-points": ["Oldie but a goodie", "Will help you rock out"]
  }

  album2 = {
    "name": "Good kid maad city",
    "category": "Hip-Hop",
    "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
    "price": 12.07,
    "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"]
  }

  var add_product = function(product) {
    $('.name').text(product.name)
    $('.category').text(product.category)
    $('.image').attr('src','http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg')
    $('.price').text(product.price)
    product.selling_points.forEach(function(point) {
      $('.description').append("<div><br>" + point + "</div><br>")
    })
  }

  add_product(book1)
  add_product(book2)
  add_product(album1)
  add_product(album2)
body {
  background-color: green;
}
#header {
  background-color: purple;
  font-size:200%;
  text-align: center;
}
#navbar {
  background-color:blue;
  text-align: center;
}
#content {
  background-color: red;
  text-align: center;
}
#book1 {
  background-color: red;
  font: black;
}
#book2 {
  background-color: red;
  font: black;
}
#album1 {
  background-color: red;
  font: black;
}
#album2 {
  background-color: red;
  font: black;
}
.image {
  height:600px;
  width:420px;

}
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <title>Blamazon</title>
  <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div id="header">BLAMAZON</div><br>
<div id="content">Products</div><br>
  <div id="book1">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
  <div id="book2">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
  <div id="album1">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>
  <div id="album2">
    <p class=name></p>
    <p class=category></p>
    <p class=price></p>
    <img class="image"></p>
    <p class="description"></p>
  </div>

<div id="footer"><div>

For future reference, open up your browsers console log and it will show you all of the errors within your Javascript.

1 Comment

Thx @Stewartside, I actually uploaded my snippet incorrectly into stackoverflow (my JS was actually in the html). I've edited my post accordingly. However, I'm still having the same issue: in ALL 4 of the divs on the page I am seeing book2's "name", "category" and "price", and book1's "selling_points". The error I see in my JS console is "Uncaught TypeError: Cannot read property 'forEach' of undefined"...
1
var book1, book2, album1, album2;

book1 = {
    "name": "Thinking Fast and Slow",
        "category": "Non-Fiction",
        "picture_url": "http://ecx.images-amazon.com/images/I/51oXKWrcYYL.jpg",
        "price": 7.07,
        "selling_points": ["This will help you make better decisions!", "Understand how humans make the wrong decisions so often"],
    add: function () {
        $('#book1 .name').text(this.name);
        $('#book1 .category').text(this.category);
        $('#book1 .price').text(this.price);
        $('#book1 .image').attr('src', this.picture_url);
        $('#book1 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#book1 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}

book2 = {
    "name": "The Life of Pi",
        "category": "Fiction",
        "picture_url": "http://bestfreebooks.org/wp-content/uploads/2015/07/Life-of-Pi-Yann-Martel.jpg",
        "price": 9.07,
        "selling_points": ["This will make you never want to get on a boat with a tiger...", "And understand the meaning of life!"],
    add: function () {
        $('#book2 .name').text(this.name);
        $('#book2 .category').text(this.category);
        $('#book2 .price').text(this.price);
        $('#book2 .image').attr('src', this.picture_url);
        $('#book2 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#book2 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}

album1 = {
    "name": "Back in Black, AC DC",
        "category": "Hard Rock",
        "picture_url": "https://upload.wikimedia.org/wikipedia/commons/b/be/Acdc_backinblack_cover.jpg",
        "price": 12.07,
        "selling_points": ["Oldie but a goodie", "Will help you rock out"],
    add: function () {
        $('#album1 .name').text(this.name);
        $('#album1 .category').text(this.category);
        $('#album1 .price').text(this.price);
        $('#album1 .image').attr('src', this.picture_url);
        $('#album1 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#album1 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}

album2 = {
    "name": "Good kid maad city",
        "category": "Hip-Hop",
        "picture_url": "http://ecx.images-amazon.com/images/I/51Zzc7PUDML._SY300_.jpg",
        "price": 12.07,
        "selling_points": ["A sprawling masterpiece of technical rapping and structured storytelling", "Defies and expands the conventions of the genre"],
    add: function () {
        $('#album2 .name').text(this.name);
        $('#album2 .category').text(this.category);
        $('#album2 .price').text(this.price);
        $('#album2 .image').attr('src', this.picture_url);
        $('#album2 .name').text(this.name);
        this.selling_points.forEach(function (point) {
            $('#album2 .description').append("<div><br>" + point + "</div><br>");
        });
    }
}

book1.add();
book2.add();
album1.add();
album2.add();

5 Comments

Thx @Eric. I cleaned up the html and just edited the version above. I understand now that the elements are getting overwritten each time. Do you know a way to write/edit the code so that I can invoke add_product(product) 4 separate times, with the specific product as the argument properly without elements being overwritten? I tried adding the qualifier as per nril's suggestion (copied and pasted his), but that resulted in a page with none of the content from the object loading at all.
I also edited my post at the same time :) Take a look above, i think that should help.
Alright, last edit. I caught a typo where you had image-url instead of matching the others that had image_url. Tested it on a fiddle, should work for you
Thx @Eric, that does work. However, I'd like to simply have to use the specific product (for example, album 1) as the argument when I invoke the function (i.e. 4 function calls for book1, book2, album1, album2).
That makes sense, then i would go with nril's example above, just make sure you declare the vars with the same name as the ids you will be placing them like you have it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.