Mark Zhou
Mark Zhou

Reputation: 13

How to simulate "add another item" via JS or Jquery in Drupal 7,

Currently, I need dynamically add items (form elements, more specifically field collection items) to form via JS or Jquery ways.

Anyone has ideas how to do this is appreciated

Upvotes: 0

Views: 810

Answers (2)

Walli
Walli

Reputation: 11

Since you want to produce the 'add another item' via javascript, that means you need to perform the click event of the button 'add another item'.

The jQuery .trigger() does not execute the action of the button #ajax. So you just need to use:

 // Base id button 'add another item'.
 base='edit-field-etape-und-add-more';
 id= '#edit-field-etape-und-add-more';
 $(id).click();
 Drupal.ajax[base].eventResponse($(id), 'click');

Upvotes: 1

The System Restart
The System Restart

Reputation: 2881

From you question I understand that, you're trying to fetch field from database and depending on those field wanna to add fields to HTML.

A possible solution:

Step 1: Send request to php file through AJAX and get the list of field [response may be HTML, JSON etc].

Step 2: If response is HTML then set the whole HTML to container/ if json then make looping on them and add them to container

Upvotes: 0

Related Questions