Chud37
Chud37

Reputation: 5007

Objects in Arrays: Unexpected String

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.

Upvotes: 0

Views: 743

Answers (1)

Mohamed Abbas
Mohamed Abbas

Reputation: 2288

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>

Upvotes: 6

Related Questions