Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am trying to use inline javascript, added with hook_alpha_preprocess_page, to add support to IE.

drupal_add_js('
jQuery(document).ready(function () {
    if (jQuery.browser.msie && jQuery.browser.version < 10) {
            $("li#megamenu-mlid-774 .megamenu-items, li#megamenu-mlid-842 .megamenu-items").columnize({
                width: 420,
                columns: 3
            });
        }
  });', 'inline');

While this loads on the page, nothing is happening. Is this because my selector starts with $? What should I change it to for this to work?

share|improve this question

2 Answers

up vote 2 down vote accepted

yes it has something to do with your selector starting with $. Your code in your question may have worked in Drupal 6. jQuery has changed in Drupal 7

Try wrapping your function like this:

(function ($) {

 // in this function, you can use the $ which refers to the jQuery object

}(jQuery));

For more information checkout

http://drupal.org/update/modules/6/7#javascript_compatibility

share|improve this answer

First of all you need to replace $ with jQuery and secondly you also need to put the width and coulmn properties inside double quotes. i.e,

drupal_add_js('
jQuery(document).ready(function () {
    if (jQuery.browser.msie && jQuery.browser.version < 10) {
            jQuery("li#megamenu-mlid-774 .megamenu-items, li#megamenu-mlid-842 .megamenu-items").columnize({
                "width": "420",
                "columns": "3"
            });
        }
  });', 'inline');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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