#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    563
    Rep Power
    51

    Help with Syntax


    Hi:

    I am pretty new with JavaScript and I'm trying to teach myself, partially by trial-and-error, modifying existing code.

    I have a form that someone else wrote and I'm trying to modify. It uses a keyup function to calculate a cost for a line-item based on quantity and sums all of the lines to get a total as the user type the quantity on one line.

    The quantity form field looks like:
    Code:
    <INPUT type=\"text\" name=\"ttl_units[$i]\" class=\"unitCount\" data-id=\"$i\"  data-p_name=\"$product_name\" data-f_name=\"$field_name\"  data-standard=\"$standard_price\" {...} size=\"10\" maxlength=\"10\" value=\"0\" /> 
    <INPUT type=\"hidden\" name=\"prod_id[$i]\" value=\"$product_id\" /> 
    <INPUT type=\"hidden\" name=\"prod_name[$i]\" value=\"$product_name\" /> 
    <INPUT type=\"hidden\" name=\"standard_price[$i]\" value=\"$standard_price\" /> 
    {... Other hidden input fields...}
    <INPUT type=\"hidden\" name=\"unit_price[$i]\" id=\"unit_price[$i]\" value=\"0\" /> 
    <INPUT type=\"hidden\" name=\"subttl[$i]\" class=\"subVal\" id=\"subttl[$i]\" value=\"0\" />
    The function that calculates cost based on what the user is typing is:
    Code:
    $('.unitCount').keyup(function(e){ 
     {.... calculates line item cost ...}
    //  Following sums all line items (I think)
    $('.subVal').each(function() { grandTotal += +this.value }); 
    }
    I'm confused by the syntax of both
    "$('.unitCount').keyup(function(e){ }"
    and
    "$('.subVal').each(function() { grandTotal += +this.value })"

    Can someone help me with that? specifically, how are the classes "unitCount' and "subVal" being interpreted

    Thanks
    Can someone help me
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,817
    Rep Power
    9646
    $() would be jQuery. That $ function, and yes that is its name (it's a shorthand alias for the actual "jQuery" function), has multiple uses but the one where you pass a string as the first argument is a way of evaluating a selector. "Selector" as in the same ones used in CSS, plus some additional features.

    $(".unitCount") finds all elements in the document with the unitCount class, and $(".subVal") finds elements with the subVal class.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    563
    Rep Power
    51
    Ah... thanks. That should get me pointed in the right direction.

IMN logo majestic logo threadwatch logo seochat tools logo