0

I have the following scriptthat runs on $(document).ready where the script alter some form elements such as radio buttons.

When the form is updated via JQuery ,the script is not run.

I need the script to alter radio buttons after successful AND unsuccessful form submissions.

Here is a link to my test site:http://test2.bratpaks.co.za/home

    (function($) {
      Drupal.behaviors.icheck = {
        attach: function (context, settings) {
          $('input[type=radio]', context).each(function(){
          var label = $(this).next(),
          var label_text = label.text();
          label.remove();
            $(this).iCheck({
            checkboxClass: 'icheckbox_line-realred',
            radioClass: 'iradio_line-realred',
            insert: '<div class="icheck_line-icon"></div>' + label_text
            });
          });
         }
       };
     })(jQuery);
  • Welcome to Drupal Answers :) This site is for questions about Drupal only. – Clive Jun 20 '13 at 11:50
  • @Clive his problem is Drupal-specific as Drupal handles javascripts in specific way. I agree it can be found easily, and question could be worded better, but it definitely IS a Drupal question. Outside Drupal his document ready would work, and for new elements Drupal provides helpers unavaliable in jQuery! – Mołot Jun 20 '13 at 12:18
  • I'm not sure what you mean there - we have a question with an offsite link, a profession of a jQuery bug, and zero code to work with. If not off topic this is then too localised. But you don't have to use js behaviours (many modules don't for very good reason) so until we see some code to show that the bug is to do with Drupal, by definition we can't assume it's a Drupal problem. FWIW his document.ready will also work inside Drupal, Drupal does nothing to override the existing jQuery implementation, it simply augments it – Clive Jun 20 '13 at 12:37
  • 1
    Mmm I get you, my bad, I am not an experienced programmer but needed help and this was my last resort. See my comment below for the code. – Studira Jun 20 '13 at 12:50
  • @Studira As I noted in the comments on the answer, if you can identify the source of the problem and post the code surrounding it (edit it into the question), plus any errors, we can try to help you out. Even if it's still off-topic for this site, there may well be somewhere else in the network we can migrate it to where you will get an answer – Clive Jun 20 '13 at 13:29
4

You should never use simple document ready in Drupal.

Drupal Behaviors was created specifically to address issues like yours. Nice entry manual can be found here.

Quick example:

(function($) {
  Drupal.behaviors.yourmodule = {
    attach: function (context, settings) {
      // your code here
    }
  };
})(jQuery);

In your code, remember to use context, like $('input[type=radio]') should become $('input[type=radio]', context)

  • Hi thanks, done that but now the labels won't show after a form submit. Here is the repo:gist.github.com/Studira/0312c0565e26e7cb28f4 – Studira Jun 20 '13 at 12:47
  • @IanJoubert don't send me your repo unless you want me to send you my bill ;) Except Open Source projects I might be willing to participate, that is, but that's pretty much another story. – Mołot Jun 20 '13 at 12:51
  • Wow...ok I get you, thanks for the help so far, thought Drupal help on Drupal is a community thing but I understand. I guess I am the only one that use Drupal for commercial gain. I included the repo so that others can maybe also use the same functionality. – Studira Jun 20 '13 at 12:55
  • 1
    I'm willing to answer questions if my answer can be useful for community. Not in someone's private projects. About using Drupal for commercial gain - well, I do it, I code for money, right? – Mołot Jun 20 '13 at 13:00
  • 1
    @Studira We've perhaps been a bit unhelpful here and not explained how the site works, apologies for that. One of the golden rules of stack exchange sites is that questions be useful for future visitors (we're different from forums in that respect). We're a bit more relaxed about that here than on Stack Overflow, say, but we still need a complete definition of the problem, preferably with code, in the question. Including external links is fine, but the question itself can't rely on those external links - the minute those links go down, it's hard for the question to retain a substantial context – Clive Jun 20 '13 at 13:25

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.