Search code examples
javascriptjqueryobjectobject-literal

Is there a shortcut way that I can set up fields of an object with javascript or jQuery?


I have the following code:

obj = new Object();
obj.close = close;
obj.$form = $form;
obj.action = $form.attr('data-action')
obj.entity = $form.attr('data-entity')
obj.href = $form.attr('data-href');
obj.rownum = $link.attr('data-row');
obj.$row = $('#row_' + obj.rownum);
obj.$submitBt = $('.block-footer button:contains("Submit")');

Is there some way that I could combine this into one assignment with jQuery?


Solution

  • It is not about jQuery. It is about JavaScript. If perfunctorily: in JavaScript object is an associative array.

    obj = {
      close: close,
      $form: $form,
      action: $form.attr('data-action'),
      entity: $form.attr('data-entity'),
      href: $form.attr('data-href'),
      rownum: $link.attr('data-row'),
      $row: $('#row_' + obj.rownum),
      $submitBt: $('.block-footer button:contains("Submit")')
    };