1

I am trying to pass a bunch of variables to a hidden input value but I am getting a stupid syntax error in this code:

$('#imgdata').append(   
    '<input type="hidden" name="imgdata[' + id + '][width]" value="' + _width + '"/>
     <input type="hidden" name="imgdata[' + id + '][height]" value="' + _height + '"  />
     <input type="hidden" name="imgdata[' + id + '][left]" value="' + _left + '"  />
     <input type="hidden" name="imgdata[' + id + '][top]" value="' + _top + '"  />
     <input type="hidden" name="imgdata[' + id + '][src]" value="' + _src + '"  />'
 );

I must be overlooking a simple syntax mistake. Console tells me its in 3rd line.

SOLUTION:

The issue was with the line-wrapping. Making the code inline without pressing enter for formatting fixed it.

mistersoftee
  • 93
  • 1
  • 5
  • 3
    Wanna enlighten us with what the console is telling you? Is it just "syntax error" with absolutely no other information? My guess is the words "Unterminated String Literal" may be in there somewhere. :) Either way, you might consider starting by making sure each line ends with a `' +` and begin 3,4,5, and 6 with a `'` as not every editor can handle line-wrapping seamlessly. – jamesmortensen Apr 02 '12 at 01:00
  • Chrome says `Uncaught SyntaxError: Unexpected token =` in line 3 while safari says `SyntaxError: Unexpected EOF` – mistersoftee Apr 02 '12 at 02:33
  • The issue was indeed a line-wrapping issue. I simply made it one line in text editor and that fixed it. Thank you. – mistersoftee Apr 02 '12 at 02:37

1 Answers1

3

JavaScript string lines must be end with \. Besides that make sure all the variables are indeed defined.

You code sample should be as follows:

$('#imgdata').append(   
    '<input type="hidden" name="imgdata[' + id + '][width]" value="' + _width + '"/>\
     <input type="hidden" name="imgdata[' + id + '][height]" value="' + _height + '"  />\
     <input type="hidden" name="imgdata[' + id + '][left]" value="' + _left + '"  />\
     <input type="hidden" name="imgdata[' + id + '][top]" value="' + _top + '"  />\
     <input type="hidden" name="imgdata[' + id + '][src]" value="' + _src + '"  />'
 );​
iambriansreed
  • 21,935
  • 6
  • 63
  • 79