Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2011, 10:41 PM   PM User | #1
Arnaud
Regular Coder

 
Join Date: Jan 2008
Location: Geneva, Switzerland
Posts: 413
Thanks: 12
Thanked 29 Times in 29 Posts
Arnaud is on a distinguished road
missing ] after element list

Hi folks!

What's wrong with this?

PHP Code:
            var addWpParams = {
                
'code' item.code,
                
'name' item.name,
                
'type' item.type,
                
'lat' item.lat,
                
'lng' item.lng,
                
'latLng' latLng
            
};

            
addWpParams(addWpParams); 
Returns an error: "missing ] after element list" in firebug

I must be doing something wrong but what?

TIA
__________________
Chuck Norris counted to infinity.
Twice.
Arnaud is offline   Reply With Quote
Old 10-15-2011, 11:09 PM   PM User | #2
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
The obvious error is that you're using addWpParams as if it were a function, which it isn't — whatever it was before, you're overwriting it with that object literal.

The error you quoted doesn't sound like it has anything to do with that, though, and there doesn't seem to be anything else wrong with that snippet you posted. Can you show this live?
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 10-16-2011, 11:35 AM   PM User | #3
Arnaud
Regular Coder

 
Join Date: Jan 2008
Location: Geneva, Switzerland
Posts: 413
Thanks: 12
Thanked 29 Times in 29 Posts
Arnaud is on a distinguished road
Well... I actually have a function that is called addWpParams...

Now I have changed both the var and the function name to something different but I still get the same error. That thing is driving me mad...

I can't really post a live example as this is a part of a big project but here is the complete part:

PHP Code:
// Function to display search results

function displaySearchResults(searchTerm) {

    
console.log("function displaySearchResults(" searchTerm ")");

    $.
getJSON("model/search.php?search=" searchTerm, function(data) {
            
        $.
each(data, function(i,item) {
            
            var 
latLng = new g.LatLng(item.latitem.lng);
            
            var 
addParams = {
                
'code' item.code,
                
'name' item.name,
                
'type' item.type,
                
'lat' item.lat,
                
'lng' item.lng,
                
'latLng' latLng
            
};
            
            var 
srContents '<div class="float-left">';
            
srContents += '<table class="sr-table">';
            
srContents += '<tr>';
            
srContents += '<td colspan="3"><img src="images/ui/arrow_right.png" /> <a href="#" onclick="addAsWaypoint(' addParams ');" class="white">Add as waypoint</a></td>';
            
srContents += '</tr>';
            
srContents += '</table>';
            
srContents += '</div>';
    
            $(
"#searchContainer").append(srContents);
        });
    });
}

function 
addAsWaypoint(params) {
    
    
console.log("function addAsWaypoint(" params ")");
    

Now the variable is called addParams and the function addAsWaypoint
Thanks in advance for any help on this!
__________________
Chuck Norris counted to infinity.
Twice.
Arnaud is offline   Reply With Quote
Old 10-16-2011, 11:51 AM   PM User | #4
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Now, the obvious error is that you can't concatenate the object addParams to that onclick string like that. It will be converted to the string "[object Object]", and you don't want that string to be the parameter for addAsWaypoint.

But, again, this doesn't look like it's causing the error you described.

If I had to guess, I'd say your JSON is invalid. If you can't show a live example, you will have to use Firebug's network tab yourself to get the actual AJAX response, and post it here.
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback
venegal is offline   Reply With Quote
Old 10-16-2011, 12:02 PM   PM User | #5
Arnaud
Regular Coder

 
Join Date: Jan 2008
Location: Geneva, Switzerland
Posts: 413
Thanks: 12
Thanked 29 Times in 29 Posts
Arnaud is on a distinguished road
I think my JSON is valid. I did several tests and had it validated with JSONLint.

Here is an example of JSON that my search.php returns:

[{"id_gmaps_navaids":"19820","code":"KLAX","name":"Los Angeles International Airport","type":"large_airport","lat":"33.94250107","lng":"-118.40799713","altitude":"125","frequency":"0","iso":"US","link":"http:\/\/en.wikipedia.org\/wiki\/Los_Angeles_International_Airport","exact":"1"},{"id_gmaps_navaids":"128819","code":"OKLAX","name":" OKLAX","type":"FIX","lat":"43.86750031","lng":"16.04277802","altitude":"0","frequency":"0","iso":"", "link":"","exact":"0"}]

It definitely seems valid to me (?)

I didn't get your explanation about the "obvious error". Do you mean I should add the onclick event to my link in another manner?

Thank you.
__________________
Chuck Norris counted to infinity.
Twice.
Arnaud is offline   Reply With Quote
Old 10-16-2011, 12:17 PM   PM User | #6
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Your JSON is ok.

What I meant before is this:

If you concatenate the object addParams to a string like this:
PHP Code:
var str '<a href="#" onclick="addAsWaypoint(' addParams ');">'
the resulting string will be this:

Code:
<a href="#" onclick="addAsWaypoint([object Object]);">
The automatic type conversion that's happening when you try to concatenate an object to a string will not stringify the object to JSON, it will result in the meaningless string "[object Object]", so, yes, you will have to find another way to add that click event (putting it inline like you tried to do is messy anyways).

And, come to think of it, this does cause the error you described, because to the interpreter [object Object] looks like an array, but isn't actually valid syntax.

Edit: This is a perfect example of why it's bad to put code into strings. If anything is wrong with that code, it will be hard to debug, because you won't get any sensible line number information.
__________________
.My new Javascript tutorial site: http://reallifejs.com/
.Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
.Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

Last edited by venegal; 10-16-2011 at 12:22 PM..
venegal is offline   Reply With Quote
Old 10-16-2011, 12:35 PM   PM User | #7
Arnaud
Regular Coder

 
Join Date: Jan 2008
Location: Geneva, Switzerland
Posts: 413
Thanks: 12
Thanked 29 Times in 29 Posts
Arnaud is on a distinguished road
Quote:
Originally Posted by venegal View Post
Your JSON is ok.
so, yes, you will have to find another way to add that click event (putting it inline like you tried to do is messy anyways).

And, come to think of it, this does cause the error you described, because to the interpreter [object Object] looks like an array, but isn't actually valid syntax.

Edit: This is a perfect example of why it's bad to put code into strings. If anything is wrong with that code, it will be hard to debug, because you won't get any sensible line number information.
I agree. It's messy and yes, it will return that error on line #1 which of course is irrelevant.

I think I got the main idea and I will try to fire that click event in another way.

Thanks for your help!
__________________
Chuck Norris counted to infinity.
Twice.
Arnaud is offline   Reply With Quote
Old 10-16-2011, 01:00 PM   PM User | #8
Arnaud
Regular Coder

 
Join Date: Jan 2008
Location: Geneva, Switzerland
Posts: 413
Thanks: 12
Thanked 29 Times in 29 Posts
Arnaud is on a distinguished road
Resolved.

I added an ID (link + i) to each link then used

$("#link" + i).click(function() {
addAsWaypoint(addParams);
});

and it now works as expected!

Thanks again for your help!
__________________
Chuck Norris counted to infinity.
Twice.
Arnaud is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:39 PM.


Advertisement
Log in to turn off these ads.