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

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-31-2012, 08:51 PM   PM User | #1
wjrasmussen
New Coder

 
Join Date: May 2008
Posts: 33
Thanks: 11
Thanked 0 Times in 0 Posts
wjrasmussen is an unknown quantity at this point
.on click syntax error

Getting a Syntax Error: missing ) after argument list.

All I want to do is make each item returned from the getJSON call to have a click event. For example. when someone clicks on an item, it might bring up a full list of details about an item. So it seems I have to pass information to the function.
Code:
$.getJSON("getproducts.php", function (json) {
    $.each(json.products, function (i, val) {
        var prodinfo = '<li>' + this['productname'] + '</li>';
        var prodcode = val.productcode;
        var sellerid = val.sellerid;
        var ordernum = val.ordernum;
        $('#productsdiv').append($('<li></li>')
            .append($('<p></p>')
            .text(val.productname)
            .hover(function () {
            $(this).addClass('hover');
        }, function () {
            $(this).removeClass('hover');
        })

            .on("click", {
            prodcode: prodcode,
            sellerid: sellerid,
            ordernum: ordernum
        }, salesorder);))

    })
});
wjrasmussen is offline   Reply With Quote
Old 11-01-2012, 02:36 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,613
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Use JSHint or a similar lint tool, it shows you issues with your code.

Let’s format your code a little better so it’s easier to read:
PHP Code:
$('#productsdiv').append(
  $(
'<li></li>').append(
    $(
'<p></p>')
      .
text(val.productname)
      .
hover(
        function () {$(
this).addClass('hover');},
        function () {$(
this).removeClass('hover');}
      )
      .
on("click", {
        
prodcodeprodcode,
        
selleridsellerid,
        
ordernumordernum
      
}, salesorder); // end on()
    
// end append()
  
// end append() 
It’s hard to spot but actually quite logical: The semicolon after salesorder) is wrong as it’s inside the append() function.

However, you could have saved all that hassle if you wrote your code a little more effectively:
PHP Code:

$('#productsdiv').append(
  $(
'<li>').append(
    $(
'<p>', {
      
textval.productname,
      
hover: function() {$(this).toggleClass('hover');},
      
click: function() {
        
// do stuff on click
      
}
    }) 
// end <p>
  
// end append()
// end append() 
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
wjrasmussen (11-01-2012)
Old 11-01-2012, 09:36 PM   PM User | #3
wjrasmussen
New Coder

 
Join Date: May 2008
Posts: 33
Thanks: 11
Thanked 0 Times in 0 Posts
wjrasmussen is an unknown quantity at this point
Quote:
Originally Posted by VIPStephan View Post
Use JSHint or a similar lint tool, it shows you issues with your code.

Let’s format your code a little better so it’s easier to read:
PHP Code:
$('#productsdiv').append(
  $(
'<li></li>').append(
    $(
'<p></p>')
      .
text(val.productname)
      .
hover(
        function () {$(
this).addClass('hover');},
        function () {$(
this).removeClass('hover');}
      )
      .
on("click", {
        
prodcodeprodcode,
        
selleridsellerid,
        
ordernumordernum
      
}, salesorder); // end on()
    
// end append()
  
// end append() 
It’s hard to spot but actually quite logical: The semicolon after salesorder) is wrong as it’s inside the append() function.

However, you could have saved all that hassle if you wrote your code a little more effectively:
PHP Code:

$('#productsdiv').append(
  $(
'<li>').append(
    $(
'<p>', {
      
textval.productname,
      
hover: function() {$(this).toggleClass('hover');},
      
click: function() {
        
// do stuff on click
      
}
    }) 
// end <p>
  
// end append()
// end append() 
I should have left the hover off as it isn't part of what I want. I used a beautifier to clean up the code and pasted it. So that's their issue.

My real problem is with the .on. What doesn't that work?

Thanks for the jshint. I fixed the issue in the real code!

Last edited by wjrasmussen; 11-01-2012 at 10:54 PM..
wjrasmussen 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 09:23 AM.


Advertisement
Log in to turn off these ads.