Search code examples
jquerydrupalobjectahah

Jquery passing variables to objects problem


Here is my snippet of code:

  $('.content .form-submit').each(function(index) {
        var id = $(this).attr('id');
        var selector = "#" + id;
  }

The above variables come out just fine. They return ajax-comments-submit-398 #ajax-comments-submit-398 (or whatever form number happens to be on the page) just fine. Problem is I need to pass the result dynamically to the following object (in place of [ID] and [SELECTOR] below.

var obj3 = { 
  "ahah": {  
    [ID]: { 
      "url": "/ajax_comments/js", 
      "event": "click", 
      "keypress": null, 
      "wrapper": "comment-form-content", 
      "selector": [SELECTOR], 
      "effect": "ajaxCommentsSubmit", 
      "method": "before", 
      "progress": { 
        "type": "1bar", 
        "message": "Please wait..." 
      }, 
      "button": { 
        "op": "Save" 
      } 
    } 
  } 
}

I have tried

var obj3 = { 
  "ahah": {  
    id: { 
      "url": "/ajax_comments/js", 
      "event": "click", 
      "keypress": null, 
      "wrapper": "comment-form-content", 
      "selector": selector, 
      "effect": "ajaxCommentsSubmit", 
      "method": "before", 
      "progress": { 
        "type": "1bar", 
        "message": "Please wait..." 
      }, 
      "button": { 
        "op": "Save" 
      } 
    } 
  } 
}

but the object reads "selector" and "id" literally, not the variable I want passed (ajax-comments-submit-398)

How would I accomplish this?


Solution

  • You have to do it like this:

    var obj3 = { 
      "ahah": {} 
    }
    
    obj3.ahah[id] = { 
      "url": "/ajax_comments/js", 
      "event": "click", 
      "keypress": null, 
      "wrapper": "comment-form-content", 
      "selector": selector, 
      "effect": "ajaxCommentsSubmit", 
      "method": "before", 
      "progress": { 
        "type": "1bar", 
        "message": "Please wait..." 
      }, 
      "button": { 
        "op": "Save" 
      } 
    }