SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    jQuery: populating checkboxes dyn. from select dropdown

    this is for a test, am not sure what they mean by "do not hard-code the resulting quarter years in the javascript":

    <select id="ddQuarters">
    <option value="1/2010">January 2010</option>
    <option value="3/2010">March 2010</option>
    <option value="5/2010">May 2010</option>
    <option value="7/2010">July 2010</option>
    <!-- etc -->
    </select>

    instructions:
    When a user selects a date from the investment cycle drop-down box, the subsequent investment values must display the next three fiscal quarters. (e.g. for "January 2010", the subsequent quarters will be Q2 2010, Q3 2010, Q4 2010; for "June 2010", the subsequent quarters will be Q3 2010, Q4 2010, Q1 2011.)

    Do not hard-code the resulting quarter years in the javascript. (empahsis mine...;-)

    The subsequent quarters should be presented with checkboxes which are all unchecked.


    checkboxes in markup:

    <input id="checkbox1" type="checkbox" value="" /><label class="chkbx" for="checkbox1"></label><br />
    <input id="checkbox2" type="checkbox" value="" /><label class="chkbx" for="checkbox2"></label><br />
    <input id="checkbox3" type="checkbox" value="" /><label class="chkbx" for="checkbox3"></label>

    the values to populate the checkboxes (value and label) have to be hardcoded somewhere, no? what am I missing????

    thank you..

  2. #2
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ('edit' feature not working again...)

    and, since markup doesn't change when u set value for chkbx dynamically, how do you check that you have set correct value for the checkbox? (don't know if it shows in Firebug, since don't know if code have there now is correct ( $('#checkbox1').attr('value', 'Q2 2010'); ) and how do I populate label? (<label for=""....></label> -- do I need to give label an id?)

    thank you..

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by maya90 View Post
    The subsequent quarters should be presented with checkboxes which are all unchecked.
    Here is an example of something being hard-coded:

    Code javascript:
    function calculateTotal() {
        var total = 0;
        total += 1;
        total += 2;
        total += 3;
        total += 4;
        return total;
    }
    calculateTotal();

    The problem with being hard-coded is that it's not flexible. If you want to start from 5 and add up to 12, you would need to change those additions and add more lines.

    Here is an example that is not hard-coded:

    Code javascript:
    function calculateTotal(start, end) {
        var total = 0,
            i;
        for (i = start; i <= end; i += 1) {
            total += i;
        }
        return total;
    }
    calculateTotal(1, 4);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •