I have a node form with multivalue field in it. I want to write jQuery code, that would add more than one value from code and do some things with it afterwards.

I was trying to get this done using clone() and correcting ID and classes of cloned elements. This does not work, since when I save entire node values from cloned widgets are not saved.

It there any way to add more field widgets to multivalue field via ajax from code and do some modifications on them afterwards?

I was trying

$(my_add_more_button).trigger('mousedown');

which adds another widget, but does not support any callback to do something with it when appears there.

Comments

nevets’s picture

The primary thing here is new elements must be added from the server side so the form API "knows"
about the elements. Drupal 7 makes this easier with the new '#ajax' setting. The example modules has some example of adding form elements this way.

slashrsm’s picture

I realized that. I was using #ajax in Field API before, but here I do not need it.

The case here is to trigger creation of another item from JavaScript code and to do some stuff with new element, when I recieve it from server. Everything I need is some Javascript/jQuery magic. No PHP and Form API.

--
JAnez

nevets’s picture

The first post suggests its a form element in which case the Form API comes into play and you need to play by its rules on adding new elements.

jaypan’s picture

If you are adding form elements, you do need #ajax. There is no way around it.

If you are not adding form elements, then it's not clear what you are trying to do. If you give some more information, we will be more able to help you.

Contact me to contract me for D7 -> D10/11 migrations.

slashrsm’s picture

I am trying to implement multiselect feature in Media's "Multimedia asset" field. It currently support only single select of an asset. That means that you must select one image/video/whatever at a time even if you have mutivalue field. That can be pretty anoying if you want to add 30+ image gallery....

I managed to change media's js code to detect multivalue field and to tell media library to enable multiple select of assets. When content is selected, I get JS array of all selected items. What I want to do now is to add all this items in multivalue field, not just one. If I want to do that I must create another N items, which must be done over ajax. I do not know how to do this last part. Everything else has been done already.

Thank you all for your help!

--
JAnez

slashrsm’s picture

Bump....

--
JAnez

sanderjp’s picture

Hey JAnez, I need to get this working aswell.

I understand you need to play by the rules of the form, definitly as a proper solution. But maybe for now we can make a javascript workaround. Just to get it working untill a better solution is found.

Can you post what you've done so far? I can have a look this week.

slashrsm’s picture

I've pushed my work to a sandbox project. All changes are in javascript/media.js.

http://drupal.org/sandbox/slashrsm/1144920

I'm posting this to #951004: Allow selecting of multiple media items for a multi value media field in the same dialog. I think we should use that issue for this.

Hope we can figure something out.

--
JAnez