0

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

2
  • Where are you starting, and what have you attempted? Commented May 14, 2012 at 5:56
  • The jQuery .append() method doco has some examples of adding elements in a general sense, so perhaps you can figure out from there specifically how to add form elements? Commented May 14, 2012 at 6:05

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me. Unfortunately I can't figure out if there's a callback and how to use it.
0

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

1 Comment

Thanks. I solved the problem by just triggering the button by .mousedown() However, your solution probably not gonna work as the whole postdata is huge and cant construct it via human code. Thank you for instant reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.