1

I have created a registration form page using the webform module. The registration form page contains personal info and billing info groups with fisrtname, lastname, email, age fields and a check box saying "billing info same as personal info". Where should I write a jQuery script to put all the personal info in billing info fields when the check box is checked?

  1. I have created a js folder/check_box.js file in the bartik theme folder which i am using
  2. then made changes in bartik.info file as scripts[]=js/check_box.js
  3. then added drupal_add_js in template.php
  4. cleared all caches

but still I cannot execute my script which I have written for check box or the fields.

$("#checkbox_id").change(function() {
  if(this.checked){
    alert("checked");
    });
})(jQuery); 

This is my code to check whether its working or not. Is it correct? If not please help me with the code.

  • 1
    Do you know for sure that your script is not running? It is possible that it is running but it doesn't work. Do you have any javascript errors on the page? Posting your code is a good idea also, it will help other people to give the best possible answers. – rooby Nov 22 '14 at 6:49
  • 1
    I would not recommend modifying bartik directly. See drupal.stackexchange.com/questions/59054/why-dont-we-hack-core for the reasons why. If you want to use bartik and modify it, you can either create a sub-theme (drupal.org/node/225125) and make bartik the base theme, or copy bartik to sites/all/themes and then change the name of it and then customise all you want. Personally I would recommend creating a sub theme over copying the theme and renaming. – rooby Nov 22 '14 at 6:54
  • This sounds much more like functionality than visual design so I would say it belongs in a custom module instead of the theme but that's a bit off topic here. Once you get it working in the theme you can easily move it to a module. – rooby Nov 22 '14 at 6:54
  • $("#checkbox_id").change(function(){ if(this.checked){ alert("checked"); }); })(jQuery); i wrote this code to check whether the script is working or not.is it right? @rooby – 39467 Nov 23 '14 at 8:10
0

First off, as rooby very accurately suggested in the comment section, you should not tamper with the Bartik theme directly. You have many possibilities on where to include a js script. You can read all of them in the relevant documentation in drupal.org.

The closest to what you describe is to subtheme Bartik and put all necessary js there, or even create a small custom module.

Apart from that, jQuery in Drupal uses the noconflict mode. Try to avoid the dollar sign but rather call jQuery. Although in your particular code should be ok as it is, there may be other instances in your script using $ that would prevent it from working. More on that here. Check your browser for js errors.

Last, there is no way for us to evaluate your jQuery code if it works properly or not as we do not have the produced HTML output nor the context of your provided js code.

|improve this answer|||||

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.