0

I have a very simple js attached to a form, and I cannot see why it is not executing.

(function ($) {
    alert("test");
    $('#field-ai-event-date-unique-add-more-wrapper').hide();
    $('#field-ai-event-date-range-add-more-wrapper').hide();
})(jQuery);

The alert is popping up as expected, the jQuery instructions are not executed. They work fine in the console and hide the form elements as expected.

What is the problem here?

1
  • Well, could you please tell us what is the problem, that is what is happening? "Not executed" may mean many things, like control never reaches that point and even simple alert does not work (OK, that you excluded), there may be some error message in console (what message), they may get executed before '#field-ai-event-date-unique-add-more-wrapper is created, and so on... Last but not least, why aren't you using behaviors?
    – Mołot
    Commented Sep 5, 2013 at 13:52

1 Answer 1

2

jQuery 101: If you want to manipulate an element in the DOM, the DOM needs to be ready first.

(function ($) {
  $(function() {
    $('#field-ai-event-date-unique-add-more-wrapper').hide();
    $('#field-ai-event-date-range-add-more-wrapper').hide();
  });
})(jQuery);

See jQuery.ready().

If you can convince yourself never to forget this small fact, life will be a lot easier :)

1
  • 1
    Indeed! Thanks Clive I knew it was most certainly a newbish omission on my part! Lesson learned, I appreciate bro ;) Commented Sep 5, 2013 at 14:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.