You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: src/guides/v2.3/javascript-dev-guide/javascript/js_init.md
+57Lines changed: 57 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -31,6 +31,63 @@ In Magento 2, you have two options for specifying declarative notation:
31
31
32
32
> This is used to target either a CSS selector or `*`. If the CSS selector matches multiple HTML elements, the script will run for each matched HTML element. For `*`, no HTML element is selected and the script will run once with the HTML DOM as its target. This method can be implemented from anywhere in the codebase to target any HTML element. This is preferred when direct access to the HTML element is restricted, or when there is no target HTML element.
33
33
34
+
Consider the example of adding a custom carousel JS:
35
+
36
+
1. Copy the `<carousel_name>.carousel.js` file to the `app/design/frontend/<package_name>/<theme_name>/web/js/<carousel_name>/` directory.
37
+
1. Add your RequireJS module at `app/design/frontend/<package_name>/<theme_name>/web/js/carousel.js`.
38
+
39
+
```javascript
40
+
define(['jquery','<carousel_name>'], function($)
41
+
{
42
+
returnfunction(config, element)
43
+
{
44
+
$(element).<carousel_name>(config);
45
+
};
46
+
});
47
+
48
+
1. Add the RequireJS config to the `app/design/frontend/<package_name>/<theme_name>/requirejs-config.js` file.
#### Declarative notation using the `data-mage-init` attribute {#data_mage_init}
35
92
36
93
Use the `data-mage-init` attribute to insert a JS component in a specified HTMLelement. The following example inserts a JS component in the `<nav/>` element:
0 commit comments