var counter = 2; function...">

Expected expression getting script end

So, I have the code below in the webpage header:

<script type="text/javascript"> var counter = 2; function addNewItemField(divName) { var newDiv = document.createElement("div"); newDiv.id = "item_listing_" + counter; newDiv.innerHTML = "<label for=\"item_" + counter + "\">Item: </label><br />"; newDiv.innerHTML += "<input type=\"text\" id=\"item_" + counter + "_category\" list=\"list_categories\" name=\"items[]\">"; newDiv.innerHTML += "<input type=\"number\" id=\"item_" + counter + "_amount\" mine=\"0.00\" step=\"0.01\" value=\"0.00\" name=\"amounts[]\"><br />"; document.getElementById(divName).appendChild(newDiv); counter++; } </script> 

I try to call it with a button, however I always get a syntax error that says "expected expression, end of script".

I ran it through Linter and did not find errors, I looked through it a hundred times and I can not find where the error is. I don’t like just sending the code and asking, "Why is this not working?" but I don’t know what is happening, so I’m at a loss even how to ask the right question.

UPDATE

Here's the linked HTML snippet down the page where the function call is made, and managed peaks:

 <div id="item_listings"> <div id="item_listing_1"> <label for="item_1">Item: </label><br /> <input type="text" id="item_1_category" list="list_categories" name="items[]"> <input type="number" id="item_1_amount" min="0.00" step="0.01" value="0.00" name="amounts[]"><br /> </div> </div> <br /> <input id= "add_new_item" type="button" onClick="javascript:addNewItemField("item_listings")" value="Add Another Item">') 
+5
source share
1 answer

onClick="javascript:addNewItemField("item_listings")" is full of errors.

You cannot mix and match double quotes this way. You need to use single quotes inside double quotes or you just stop the attribute of the HTML element before.

Right now, this is analyzing how

 <input id= "add_new_item" type="button" onClick="javascript:addNewItemField(" 

... and then a bunch of trash.

You need to use single quotes:

 <input id= "add_new_item" type="button" onClick="javascript:addNewItemField('item_listings')" value="Add Another Item"> 
+13
source

Source: https://habr.com/ru/post/1215496/


All Articles