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?

  • 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 Sep 5 '13 at 13:52
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
    Indeed! Thanks Clive I knew it was most certainly a newbish omission on my part! Lesson learned, I appreciate bro ;) – stefgosselin Sep 5 '13 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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