0

I am getting the above error in this code:

var inputs = {
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID),
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID),
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID),
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID),
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID),
    $("<input>").attr({"type":"hidden","name":"collegeID"}).val(collegeID) 
}

This looks pretty valid to me. I want to .append() the variable on to a div and I dont want to have to create a bunch of different variables to do that.

5
  • 2
    Object syntax is { key: value } Commented Sep 25, 2017 at 11:16
  • 2
    You use square brackets ([]) for array literals. Curly brackets are for objects meaning it expects key-value pairs. Commented Sep 25, 2017 at 11:16
  • objects needs to have keys and values Commented Sep 25, 2017 at 11:16
  • Your creating an object literal from a list of objects. I guess you want inputs = [] to create an array? Commented Sep 25, 2017 at 11:16
  • @MikaelLennholm Thanks! That worked straight away lol, I didn't know that in Javascript. Commented Sep 25, 2017 at 11:17

1 Answer 1

6

inputs need to be an Array. use [] instead of {}

var collegeID = "";
var inputs = [
  $("<input>").attr({
    "type": "hidden",
    "name": "collegeID"
  }).val(collegeID),
  $("<input>").attr({
    "type": "hidden",
    "name": "collegeID"
  }).val(collegeID),
  $("<input>").attr({
    "type": "hidden",
    "name": "collegeID"
  }).val(collegeID),
  $("<input>").attr({
    "type": "hidden",
    "name": "collegeID"
  }).val(collegeID),
  $("<input>").attr({
    "type": "hidden",
    "name": "collegeID"
  }).val(collegeID),
  $("<input>").attr({
    "type": "hidden",
    "name": "collegeID"
  }).val(collegeID)
]

console.log(JSON.stringify(inputs));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

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.