Questions tagged [javascript]

Use it for questions about JavaScript code that is very specific for Drupal.

Filter by
Sorted by
Tagged with
34 votes
4 answers
49k views

How do I update jQuery to the latest version I can download?

I'm using Drupal 7, which uses jQuery 1.4 as default? How can I update it to the latest version (currently 1.7)?
  • 1,043
59 votes
2 answers
46k views

Better understanding of Drupal behaviors

I read about Drupal behaviors today, and I tried writing the following code. (function ($) { Drupal.behaviors.mymodule = { attach: function (context, settings) { $('#mymodule_id', ...
  • 3,034
42 votes
3 answers
50k views

how to send variable from .php file to .js file?

I hope someone is able to help me. The problem is the following one: 1)I have module in which a assign the variable in order to send to the template file php.tpl <?php ... $testvar="Hello from ...
  • 941
9 votes
2 answers
20k views

How do I add JavaScript to a site?

How can I add JavaScript and CSS to my site?
  • 223
31 votes
2 answers
35k views

Eliminate render-blocking JavaScript and CSS in above-the-fold content

I am building a Drupal 7 site. These are some of the modules I use: Adaptivetheme (theme), Views and Content Panes, Panels, Mini panels, Panels everywhere, Page manager, Superfish for menus, Addthis, ...
  • 1,417
12 votes
1 answer
11k views

How to Trigger Existing, Non-Form Ajax from JavaScript Event

I'm trying to figure out AJAX in Drupal 8. I have a custom module that works, using AJAX to change the content of a div when a link is clicked. The API says: Many different events can trigger ...
  • 145
6 votes
2 answers
14k views

How to load a node edit form using Ctools module in Drupal 7

I am trying to load a node edit form into a modal frame using Ctools. The following is the function I am using. So far the $op = 'view' part is working fine, but I cannot get the node edit form to ...
  • 879
3 votes
2 answers
6k views

How do I properly pass values from PHP to JavaScript?

I am currently writing a module and I am trying to pass some variables to the Drupal.settings. My module uses the following code. function bcc_preprocess_views_view(&$vars) { drupal_add_js(...
  • 702
2 votes
4 answers
5k views

Change module's weight - by module or manualy

I am looking for solution how to change particular module's weight in D7. I know that it is possible to do in config tables in database, but I don't know how. Another solution that I found in Drupal....
28 votes
7 answers
53k views

How to programmatically trigger a click on an AJAX enabled form submit button?

I'm trying to programmatically (with jQuery) trigger a click on button with AJAX behavior in a Drupal form, but so far jQuery('#edit-submit').click() doesn't do anything. A real mouse click on that ...
  • 1,225
15 votes
5 answers
11k views

How to prevent behavior-attached functions from being attached twice?

I have a behavior that adds on to some checkboxes. (function($) { Drupal.behaviors.mymodule = { attach: function (context, settings) { $('.skip-line', context).on('change', function(){ ...
  • 21.8k
11 votes
5 answers
22k views

hook_preprocess_page(): how to tell a views page?

Im writing a custom module that need to insert some javascript and css files only in a Views page. Im using hook_preprocess_page, but I can not tell if the current page is from a view: function ...
  • 890
10 votes
4 answers
21k views

How to make form buttons call only javascript?

I'm experimenting with JavaScript and Drupal forms. Currently I'm trying to make a button in the administration form of a module that will use JavaScript to append options to a select list. The ...
  • 669
7 votes
4 answers
4k views

Sending a value to JavaScript

I have set theme handler for a form in Drupal and this handler has a template file. What is the easiest way to send a value from PHP to JavaScript in that include file?
  • 417
3 votes
2 answers
6k views

How to override Drupal.ajax.prototype.beforeSend?

I need to add my own little if's and checks in 3 functions inside misc/ajax.js ( Drupal.ajax.prototype.beforeSend, Drupal.ajax.prototype.success, Drupal.ajax.prototype.error ) How can I ...
  • 21.8k
2 votes
2 answers
3k views

how to add javascript with #attached and hook_page_alter

I tried to add js to pages by means of #attached, as it seems to be the recommended way. From what I understand, this involves altering a render array. hook_page_alter seems to me like good candidate. ...
  • 500
29 votes
5 answers
26k views

How do I add JavaScript specific for a view?

I have a view in my site that has a specific class name. I want to know, inside the template.php file of a theme, how I can know if a view with specific class name is available in the requested page. ...
  • 1,302
19 votes
4 answers
9k views

Easiest method for conditional fields in a form

What is the easiest way to get some javascript magic for enabling/disabling form fields based on another field's value? This sounds like something there should be an helper for somewhere, but I can't ...
  • 578
17 votes
5 answers
16k views

Add JavaScript files to the administrative pages

How do I add JavaScript/CSS files in every administration pages, using a module?
  • 704
12 votes
1 answer
13k views

How do I use a jQuery version different from the default one used by core?

I need to use a different jQuery version. Currently, Drupal is using jQuery 3.2.1, but I need a previous version. In Drupal 7 you could easily change jQuery version with the jQuery Update module. How ...
  • 133
11 votes
4 answers
20k views

How to create modal windows (pop-ups)?

I am trying to use Drupal 8's core function to open a page in a modal window. Unfortunately it is very hard to find some official documentation about it, and most of the blogs covering this topic seem ...
  • 2,008
11 votes
1 answer
29k views

Pass variable to drupalSettings

I'm using Drupal 8. What I'm trying to do is: Access the active node From the node get the value of field_test and assign it to a variable Pass the variable to the drupalSettings function and ...
  • 463
9 votes
5 answers
8k views

Delay ajax call on textfield to allow for typing

I would like to delay ajax from firing in the same way that the autocomplete seems to work. For example, if a user it typing, the ajax will not run until 500ms has elapsed since the last keyup. I am ...
9 votes
1 answer
4k views

How can I force a JavaScript library to load in the <head> of a page?

How do I force a JavaScript library to load in the < head > in Drupal 8? Here's my themename.libraries.yml file: zaccordion: version: 1.x js: js/jquery.zaccordion.js: { header: true } ...
  • 2,453
7 votes
7 answers
20k views

How can I trigger a javascript function when a Drupal ajax call is complete?

I need to do some extra things after Drupal ajax is done inserting some stuff into the DOM. With jQuery this was brain-dead simple, but I can't for the life of me figure out how to do it in Drupal 7 :...
  • 1,281
6 votes
4 answers
12k views

ReferenceError: Drupal is not defined

I am working on a production site, moving settings from a development site for a mobile environment. However, I am receiving the above error. I can not figure out what is causing this error. It does ...
  • 63
6 votes
2 answers
6k views

How to extend or "hook" Drupal Form AJAX?

I'm working with standard admin form, which has 'add_more' button for one of fields. My task is to move some fields to jQuery UI Dialog just for usability. When dialog was closed I manually append it'...
  • 7,939
6 votes
2 answers
13k views

How to add CSS and JS files on node pages from theme's template.php

I am trying to add JavaScript and CSS files to one node type page from my template.php file but it is not working. Here is my template.php code: <?php // $Id: template.php,v 1.13 2010/12/14 01:...
  • 161
5 votes
2 answers
17k views

jquery once onload [closed]

Because of an external module by click on form submit the site gets called 5 times. I don't know how to use once correctly! (function ($) { Drupal.behaviors.myFunction = { attach: function (...
  • 460
5 votes
1 answer
2k views

Add inline JavaScript to end of page dynamically

Use case: I have a custom block: /** * Provides a 'Hello' Block * * @Block( * id = "my", * admin_label = @Translation("My Block"), * ) */ class MyBlock extends BlockBase { /** * {@...
3 votes
1 answer
2k views

Multiple nodes in one slide using views slideshow or another module

I have a slideshow using views_slideshow which displays the latest post from 4 different content types, I need the slide to show multiple contents (from different content types) in one slide, when you ...
3 votes
2 answers
3k views

Changing file name on upload

I'm trying to automatically add prefixes to uploaded file names so they can later be processed by non-drupal scripts. I want to do that while the user is uploading files during creation/editing of a ...
  • 513
2 votes
3 answers
2k views

Using custom JavaScript

Is there a location where I can drop in a custom JavaScript file in Drupal 7.x? It just includes simple event actions for menus, and I don't want to edit the theme/module files. Or do I have to just ...
2 votes
3 answers
5k views

How to add default text that clears on focus with jQuery to a search form text field in Drupal

I would like to have a default text in an Exposed Views Search form that says something like 'type in a keyword to search' which will disappear if the user clicks on the text field and reappear if the ...
  • 5,041
2 votes
2 answers
8k views

How can I remove Javascript files?

I wanted to remove some JavaScript files, since they conflict with my custom JavaScript on my mobile theme. I tried with the following code, but it is not working in hook_preprocess_page(), which I ...
  • 1,831
2 votes
1 answer
5k views

how to set and unset javascript files for specifc path aliases in Drupal 7?

I am trying to set particular javascript files for a specific node page by getting the path alias. like this: $path=drupal_get_path_alias(); if($path == 'about-us') drupal_add_js(drupal_get_path('...
  • 365
2 votes
1 answer
5k views

jquery script on view not working when filtering with (ajax on) 'better_exposed_filters' module

i have a script for dealing with node images hover (function ($) { $(function(){ $('.node-photos-teaser').mouseenter(function() { $(this).addClass('photos-hover'); }).mouseleave(...
  • 3,109
1 vote
1 answer
1k views

Remove jQuery and find out what is asking it to be included

I'm using drupal 7 and I simply don't want jQuery included at all. It seems like most people are advocating modify the js in a theme hook. That's fine I suppose, but it seems like this is dangerous ...
  • 767
1 vote
1 answer
4k views

EntityMalformedException: Missing bundle property on entity of type node in modal

I want to open create content type in modal, according to modal form and modal api example, I implement it, I test the problem is here when I test it by contact form ctools_include('modal'); ...
  • 21.4k
35 votes
4 answers
18k views

How do I change the ajax loading behaviour in Views?

I have a view with some exposed filters in a block; it uses AJAX and the auto-submit feature so the view is re-loaded as soon as a filter option is changed. In the short delay between selecting an ...
  • 165k
19 votes
12 answers
30k views

How to add Jquery plugin to Drupal?

Should I just paste the code in the main template file, or is there any other recommended way of attaching new Jquery plugins in Drupal 7?
  • 713
13 votes
2 answers
19k views

How can I return actual JSON using Drupal?

I'd like to implement a simple AJAX function locally that allows me to autocomplete node titles of already existing nodes as the user types. To that end, I need the ability to have an API that I can ...
  • 941
12 votes
2 answers
10k views

load a node content into a lightbox2 (without headers and footer and other blocks)

is it possible to load a node (from an id) into a lightbox 2 without having to load all the additional blocks ? just what the node has to display I want to make some popups on some links to allow ...
  • 1,046
10 votes
3 answers
14k views

How do I add CSS & JavaScript to <head>?

I want to add the following code in my section of the front page using a Drupal 8 sub theme of Bartik. Can anybody help how to do it? What is the best way to do it in this new twig version of theme?...
  • 101
10 votes
5 answers
12k views

How to add conditional javascript to theme?

I'd like to use Selectivizr script to emulate CSS3 selectors in IE6-8. But I'm having a hard time adding it to my theme. So far tried: 1) Forcing this into html.tpl.php head section: <!--[if lt ...
  • 1,853
9 votes
5 answers
32k views

What's the event triggered after an autocomplete has run and how to retrieve the value selected

I have a node add form with a term reference that uses autocomplete widget. I want the "tid" of the filled term by the user using the autocomplete widget.
  • 1,134
8 votes
3 answers
26k views

How do I pass variables to JavaScript?

In your MODULENAME.module file: $testVariable = 'himanshu'; drupal_add_js(array('MODULENAME' => array('testvar' => $testVariable)), array('type' => 'setting')); drupal_add_js(drupal_get_path('...
  • 4,309
8 votes
5 answers
13k views

Adding JS to a Drupal 8 theme (replacement for drupal_add_js)

In Drupal 7, I can use drupal_add_js within a theme's template.php file as a theme_preprocess_html(&$vars) function: drupal_add_js(drupal_get_path('theme', 'mytheme') . '/js/scripts.js', ...
7 votes
3 answers
11k views

How to pass PHP variables to Javascript/jQuery? [duplicate]

I found this way to send variables from PHP to Javascript. Inside the template.tpl.php file, I have the following code. <?php echo '<div class="drupal-vars" style="display:none;">'....
6 votes
1 answer
8k views

Pass php variables to js without drupal behaviour

I stand on the following tricky situation: I have a node with geofield coordinates attached. I am constructing an array of points. Something like: Array ( [86] => Array ( [0] => ...

1
2 3 4 5 6