Hi I have a multi value cck field in my cck content type. I want to simulate click on "add another item" using jquery. which is like

$('#edit-field-supp-quan-field-supp-quan-add-more').trigger('click');

but it causes whole content form to submit instead of adding extra multi value cck field.

Manuall clicks are working perfectly. Can anyone tell me why behavior of manual clicks and simulated clicks are different.
thanks

Comments

auth’s picture

Hi,

I wanted to do the same thing some time ago and realized that the add more button is bound to the mousedown event. So if you do

$('#edit-field-supp-quan-field-supp-quan-add-more').trigger('mousedown');

you should have more luck.

ionmedia’s picture

thank you, auth.
$('#edit-field-supp-quan-field-supp-quan-add-more').trigger('mousedown'); works fine

gapa’s picture

Is this the whole code? I tried to use this also but I just can get it to work.
This is my code in module_form_alter function:

drupal_add_js('$("#edit-group-delo-group-delo-add-more").trigger("mousedown")', 'inline');

I also tried this code which works fine:

  drupal_add_js('alert("Hello!")', 'inline');

Any ideas?

gapa’s picture

I tried this code with firebug and it works. So my next question is where can I put this code to make it work? I tried in module_form_alter and also in $form['#after_build'] function but it is not working.

jem500’s picture

hello all,

I have the same problem. Where can I put this kind of code ?

Thanks !

glass.dimly’s picture

Old thread, but I had to use
$('#add-more-id').mousedown();
to successfully trigger the "add another item" to a field.
These didn't work:

.trigger('mousedown');
.trigger('click');
.click();
ionmedia’s picture

and next issue: i need to trigger this button twice, if i just add code twice i will have only one added fieldset, but i want to add it twice, maybe i need some delay between 1st and second call?

ionmedia’s picture

now i use

    setTimeout(function(){jQuery("#edit-group-club-entry-options-group-club-entry-options-add-more").trigger(\'mousedown\');},10);
    setTimeout(function(){jQuery("#edit-group-club-entry-options-group-club-entry-options-add-more").trigger(\'mousedown\');},5000);
    setTimeout(function(){jQuery("#edit-group-club-entry-options-group-club-entry-options-add-more").trigger(\'mousedown\');},10000);

this work, but there is too long delays, small delays not working

maybe exist trigger that called after drupal ahah request complete?

zepner’s picture

... for some reason, in your hook_*_form_alter

<?php
$fieldinfo = field_info_field($form['field_noderef']);
if ($fieldinfo['cardinality'] == '-1') {
  $form['field_noderef']['und']['add_more']['#ajax']['event'] = 'click';
}
?>