Nikola Lošić
Nikola Lošić

Reputation: 479

can't find error in my code?

I am making a simple search code. I can't find error. The error message says Uncaught SyntaxError: Unexpected identifier on javascript line 40 (target=document.getElementById("outputPlace").

Do not look at the button, I have not added event listener to it yet. I just want that when I press enter products are displayed.

HTML CODE

<html>
<head>

<title>Price List </title>

</head>

<body>

<h1> PRICELIST </h1>
<form id="formSearch">
<div>
<label for="searchBox"> Search products here: </label>
<input type="text" placeholder="Type text here to search product" id="searchBox">
</div>
<div id="buttons">
<button id="getAll"> GET ALL PRODUCTS</button>
</div>

</form>

<div id="outputPlace">

</div>


<script src="product.js"></script>
</body>


</html>


JAVASCRIPT CODE

(function(){                    //start anonymous function

var list= {

  "listOfProducts": [

  {
  "name":"hard disk",
  "price": "50$",
  "quality":"good",
  },
  {
  "name":"monitor",
  "price":"100$",
  "quality": "very good",
  },
  {
  "name":"speakers",
  "price":"20$",
  "quality": "not bad",
  },
  {
  "name":"headphones",
  "price":"12$",
  "quality":"bad",
  },
  {
  "name": "mobile phone",
  "price": "300$",
  "quality": "excellent",
  },
  {
  "name": "usb memory",
  "price": "30$",
  "quality": "the best",
  }
  ]
},

var target=document.getElementById("outputPlace"),
    searchForm=document.getElementById("formSearch"),
    productList=list.listOfProducts,
    listLength=productList.length,
    searchValue=document.getElementById("searchBox"),
    searchInput=searchValue.value;




var listMethods = {

searchList: function(event) {

event.preventDefault();
var i;
target.innerHTML="";
if(listLength>0 && searchInput!=="") {

   for(i=0;i<listLength;i++) {
   var product=productList[i],
       whatIsFound=product.name.indexOf(searchInput);
       if(whatIsFound!==-1){

       target.innerHTML+='<p>'+product.name+', '+product.price+', '+product.quality+'<a href="http//www.facebook.com">click here to buy</a></p>'
       }

   }


}





},





};

searchForm.addEventListener("submit",listMethods.searchList,false);








}) (); //end anonymous function

Upvotes: 0

Views: 474

Answers (3)

Pavel Rybin
Pavel Rybin

Reputation: 46

And you have one more comma at the end of script, after }

Upvotes: 0

mikach
mikach

Reputation: 2427

Change this

var list = {
   ...
},

var target=document.getElementById("outputPlace"),

to this:

var list = {
 ...
};

var target=document.getElementById("outputPlace"),

Upvotes: 1

Jason Sperske
Jason Sperske

Reputation: 30446

You have a comma after that large JSON object you defined at the top of your JavaScript, followed by another var.

var list= {
 "listOfProducts": [
 {
  "name":"hard disk",
  "price": "50$",
  "quality":"good",
 },
 ...[a bunch of stuff]...
},

var target=document.getElementById("outputPlace"),
    searchForm=document.getElementById("formSearch"),
    productList=list.listOfProducts,
    listLength=productList.length,
    searchValue=document.getElementById("searchBox"),
    searchInput=searchValue.value;

Both of the two other proposed answers would fix this (well ok Otome deleted their answer which was to drop the second var).

Upvotes: 2

Related Questions