SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic multi-dimensional associative array

    Hello,

    I'm trying to create a multi-dimensional associative array that has some of its data created dynamically. The code I've currently got is as follows (using JQuery to retrieve form element values):

    Code JavaScript:
     
    var data = {
     
     label: $('#label').val(),
     child_float: function() {  
       $( 'input[id^=child_float]:checked' ).each( function( index ) {
     
           index: $(this).val();
     
       });  
     }
     
    }

    The function is working fine, as I tested it on its own debugged to execute alert prompts. The aim here is for the child_float member to have child members 0, 1, 2 etc. In other words for it to look something like..

    Code JavaScript:
    child_float: {
      0: 'left',
      1: 'right'
    }

    .. if its values were not being created dynamically. However as you can tell that isn't what is being generated at the moment, as there's no opening/closing brace before/after the child_float's member variables. Any ideas what changes I need to make in order make the child_float behave as intended?

    Much thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?

  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)
    You would need to perform the function behaviour later on, so that it can populate the data fields.

    Something like the following should do the job.

    Code javascript:
    var data = {
        label: $('#label').val(),
        child_float: []  
    };
    $( 'input[id^=child_float]:checked' ).each( function( index ) {
        data.child_float[index] = $(this).val();
    });

    Edit:

    It might also me possible to use jQuery's makeArray to streamline the process, but I haven't investigated that yet.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome - that worked a treat!


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
  •