Passing object (array) ob form element IDs as function parameter and accessing via jQuery

Passing object (array) ob form element IDs as function parameter and accessing via jQuery

Hi all,

up until now I have been able to pass the element ID of a form element to a javascript function and access via jQuery as follows:

  1. var elem_id = document.getElementById( 'my-form-element' );

  2. doStuff( $( elem_id ) );

  3. ...

  4. function doStuff( $elem_id ) {
  5.    $elem_id.val( 'Text' );
  6. }
...this all works fine.

However, now I have an object of element IDs that I want to pass and cannot access the individual elements via jQuery.

I have tried the following:

  1. var elem_ids = {
  2.    first_id: document.getElementById( 'my-first-form-element' ),
  3.    second_id: document.getElementById( 'my-second-form-element' )
  4. };

  5. doStuff( elem_ids );

  6. ...

  7. function doStuff( elem_ids ) {
  8.    $( elem_ids.first_id ).val( 'Text' );
  9. }
...but this does not work :( I want to be able to send the array/object of IDs so that I do not have to pass all of the element IDs as individual parameters.

Can this be done?

Thanks in advance.