Trevor Newhook
Trevor Newhook

Reputation: 909

drupal form trigger add more javascript

I have a node add form with a field that has an 'add more' button. This particular field needs to be populated dynamically. Why doesn't it work to trigger a jquery click on the 'add more' button ($('#edit-field-roof-area-und-add-more').click();)? If I do that in the console, it returns an empty array.

What is the simplest way to create one or two fields in a node add form that I can add an unlimited amount to dynamically from the client side (the values come from a JS application.)

Upvotes: 0

Views: 1806

Answers (2)

Trevor Newhook
Trevor Newhook

Reputation: 909

found it!! jQuery('#edit-field-phone-no-und-add-more').trigger('mousedown') Also, the ID changes each time the button is clicked.

Upvotes: 3

Jonathan Marzullo
Jonathan Marzullo

Reputation: 7041

when adding nodes you might want to look into using context in your handler:

$('#edit-field-roof-area-und-add-more', 'body').click();

since the elements i dynamically created after the DOM is loaded.. try that, since the body tag is already present in the DOM when the page loads

and/or provide a jsfiddle or codepen example to better help you more...

EDIT:

you could also try this jQuery trigger():

 $('#edit-field-roof-area-und-add-more', 'body').trigger("click");

or jQuery triggerHandler():

 $('#edit-field-roof-area-und-add-more', 'body').triggerHandler("click");

the difference between the two is that triggerHandler does not bubble up the DOM

also do have an example jsfiddle or codepen.. of your click event handler you are trying to trigger?

Upvotes: 0

Related Questions