I use CCK and made a node reference field that I use in a Form.
I need to ADD an attribute to the input object when it renders in a web page.
How is this done?

Here is my example:

<label for="edit-field-org-node-reference-0-node-name">Organization: </label>
 <input type="text" maxlength="128" name="field_org_node_reference[0][node_name]" id="edit-field-org-node-reference-0-node-name"  size="60" value="" class="form-text form-autocomplete" />

needs to become the same input field but an added "attribute" that runs a javascript function onchange... like this:

<label for="edit-field-org-node-reference-0-node-name">Organization: </label>
 <input type="text" maxlength="128" name="field_org_node_reference[0][node_name]" id="edit-field-org-node-reference-0-node-name"  size="60" value="" class="form-text form-autocomplete" onChange="lookupinfo(this.value)" />

See that last attribute?

I tried following this but got stuck: http://drupal.org/node/117346
and this: http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

My attribute works great outside of Drupal in straiht html calling an embedded javascript, but I am having difficulty in how to add this attribute javascript call INTO my existing CCK custom node reference field. Any ideas anyone?

Thanks in advance for thinking about this. :-)

Comments

iansears’s picture

I may have found THE thread to answer this, though I haven't proven to myself yet.
http://drupal.org/node/280408

Hope this helps anyone looking for answers on this.

Ian

iansears’s picture

No answers at all? Please, if you have any ideas or even rough pointers for me please comment.

kswan’s picture

I have been working on a similar problem. I have found something that might help you. See http://drupal.org/node/161666#comment-272621.

I think you could put something like in your hook_form_alter():

drupal_add_js("
     $(document).ready(function () {
        $('#edit-field-org-node-reference-0-node-name').change(function () {
           lookupinfo(this.value);
        });
     });", "inline");

I want my javascript applied to several elements so I want to add a class to the relevent elements that will be found by jquery. Unfortunately it appears that CCK is overwriting my #attributes property.

kswan’s picture

I think the answer is here http://drupal.org/node/336355.

The code in this comment (http://drupal.org/node/336355#comment-1150284) worked for me.

kenorb’s picture

Thanks