So I have the below code in the header of a webpage:
<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 calling it using a button, however I always get a syntax error stating "expected expression, got end of script."
I've ran it through Linter and it found no errors, I've reviewed it a hundred times and I cannot find where the fault is. I hate to just post code and ask "Why doesn't this work?" but I don't have any idea what's going on so I'm at a loss on even how to ask a proper question for it.
UPDATE
Here is the associated piece of HTML furhter down the page where the function call is made and the peices being manipulated reside:
<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">')
onClick="javascript:addNewItemField("item_listings")"
is full of errors.
You cannot mix-and-match double quotes that way. You need to use single quotes inside the double quotes or you're just stopping your HTML element's attribute early.
Right now, this parses as
<input id= "add_new_item" type="button" onClick="javascript:addNewItemField("
... followed by a bunch of garbage.
You need to use single quotes:
<input id= "add_new_item" type="button" onClick="javascript:addNewItemField('item_listings')" value="Add Another Item">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With