Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.

Questions tagged [javascript]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
5
votes
1answer
2k views

Update Commerce Cart using Ajax

Is there a way to update the cart using ajax? I've tried something like: $('span.quick-cart-add>p>a').click(function(e){ e.preventDefault(); var data = {}; ...
5
votes
1answer
210 views

Are there callbacks for elementSelect macro?

I am writing a custom field type that makes use of Craft's asset selector, and saw that there is an elementSelector macro (in includes/_forms). I want to be able to do some things onSelect, but I ...
5
votes
1answer
240 views

How to trigger updateElements when hide modal?

Is it possible to refresh an element's list when closing a modal? I have a modal ready with: onHide : function(){ //but all what I tried doesn't work here //1: This will do the trick, but ...
4
votes
3answers
1k views

JS templating in Craft CP

I'm writing a plugin where I need to render some semi-complex HTML clientside at runtime (all inside the CP). Does Garnish (or any other library/framework/JS-thingamabob shipped with Craft) offer any ...
4
votes
1answer
3k views

Loading javascript on specific sections/pages

What is the recommended way to add section specific javascript to entry pages? I need to make some 3rd party api calls front end for currency conversion but I want to only use it on a specific ...
4
votes
3answers
858 views

Creating environment variable from JSON file

Brand new to Craft here. I'm trying to port across my gulp workflow to Craft and basically everything has gone super smooth. Just one last obstacle due to my lack of knowledge of the Twig templating ...
4
votes
1answer
2k views

How can I get cookies that were set in javascript from a twig template?

The docs state that {{ craft.request.getCookie(name) }} will not work for cookies that were set in JavaScript, because all cookies in Craft go through some validation to ensure that they aren't ...
4
votes
1answer
436 views

Can Garnish be extended to allow plugins to use custom keyboard shortcuts?

For example, when editing an entry cmd + s saves the entry. Is there an easy way to extend Garnish to allow for my plugin to listen for custom keyboard shortcuts?
3
votes
2answers
987 views

Is it possible to disable dev output in a template when devMode is true?

For example the template just outputs JSON, which breaks when Craft's devMode logging is added.
3
votes
2answers
464 views

Access add and remove events with the Craft Element Selector?

Is there any way to access an event, and trigger some function, when an element is added / removed with the Craft Element Selector logic available in Garnish? I am working on a custom field type and ...
3
votes
2answers
147 views

Is there an accessible callback when Craft.EntryTypeSwitcher is run?

When a user switches Entry Types in the control panel, Craft.EntryTypeSwitcher is trigged which in turn triggers onTypeChange. Is there callback that a plugin can access to run some additional ...
3
votes
1answer
126 views

Setting the position of a SmartMap marker on the front end

I'm using SmartMap in a front end entry form and trying to set the position of a marker as the user enters their address. I can get the lat/lon lookup working but I'm stumped with the marker itself. ...
3
votes
2answers
732 views

How do you use a for loop to output to JS?

I have a map generated in JS. I need to set the country-id and url for each country. Both 'country-idandurlare fields in my Craft categories groupcountries`. { "country-id": "FR", "url": "...
3
votes
1answer
272 views

Listen for checkbox check event for assets table and modify action trigger menu item

I am able to listen to an event when the user checks the checkbox in the assets table. This is within my js file init file that extends garnish: Garnish.Base.extend When the checkbox is selected, I ...
3
votes
1answer
151 views

Swapping Hogan for other JavaScript template engine in the SearchPlus plugin

For a JavaScript novice like myself, what steps would be involved in swapping out the hogan.js templating framework for something else? I have my eye on Swig, which comes with some 'twig-like' filters ...
2
votes
3answers
23k views

Can I set a JS variable to a Twig output tag?

This came out of a previous question I need to set a variable of a tag group that I can reference in jquery for use with a plugin. I'm declaring this variable in a tag so my included js files can ...
2
votes
3answers
338 views

Triggering JavaScript from back-end event

I'm building a plugin and want to trigger some JavaScript on an event being fired (eg an entry being saved). What is the best way to achieve this? Coming up short looking through the documentation and ...
2
votes
2answers
2k views

Craft 3 - ajax request to plugin

This is a pretty simple test before I implement some larger functionality. Eventually this will be just a glorified upload form via ajax. Part 1 - Ajax post $.ajax({ type: 'POST', ...
2
votes
1answer
829 views

AJAX page transitions with history.pushState

I'm a new craft user (previous EE user). trying to follow Zack Spear's directions (https://designbycosmic.com/journal/craft-cms-ajax-page-transitions-with-history-pushstate) for how to setup Ajax page ...
2
votes
1answer
1k views

Can you pass a variable with includeJsFile?

Can you do something similar to using "with" in twig's include tag (http://twig.sensiolabs.org/doc/tags/include.html), but instead with includeJsFile? I am using a plugin to filter my entries by ...
2
votes
1answer
320 views

Trigger Task Status Icon

I have a plugin that starts a task and subtasks from a CP widget AJAX call. The tasks are running without a hitch, but for some reason on production, the icon does not appear when the tasks start ...
2
votes
1answer
928 views

Local storage in Twig

Quick question: Is it possible to access the browser's local storage in Twig? In JavaScript you would do something like: localStorage.getItem('myItem'); Thanks!
2
votes
2answers
2k views

Add listener to dynamic html elements

I have a field type that I need to initialize. When the page loads I can use code like the following to handle certain behaviors of the field: $('.myfieldwrapper').each(function() { new Craft....
2
votes
1answer
508 views

Including plugin javascript on the client side

I've created a simple plugin based on the Craft docs with the following directory structure... /craft /plugins /eforms /resources /js ...
2
votes
1answer
183 views

How would I trigger updateElements for BaseElementIndex from a plugin's JavaScript?

If I wanted to update the Elements listing based on an action taken from a plugin, would it be possible to do this? Setup: We're on an Elements listing page. We filter the results with a search ...
2
votes
2answers
49 views

How can I add Google MarkerClusterer library to Smart Map?

I started to work with Craft CMS a couple month ago, I have a client that wants to have a clustered map, I went over Smart Map documentation but I couldn't find anything about on how to add Google ...
2
votes
2answers
833 views

Translating static text in JavaScript files

I've got some static text that is dynamically being displayed on the frontend with JavaScript. Now considering the site has 3 locales, how do I best translate those static texts? I know I could ...
2
votes
1answer
739 views

Use Craft.postActionRequest in frontend template

Until now I've always done backend plugin development but my next task is to create a homepage with craft. To make an Ajax request I used Craft.postActionRequest but if I try this in my frontend ...
2
votes
1answer
68 views

Is there a JS event triggered when a new widget is created?

Pretty straightforward problem... On the dashboard, I click "New Widget". I select "My Custom Widget" type. A new instance of "My Custom Widget" appears on the dashboard, ready to be edited. How can ...
2
votes
1answer
162 views

JS - Determine that an entry is unsaved?

I've created a custom fieldtype... which is basically a button that triggers an AJAX action. When I add this field to my section, I can click the button while I'm editing an entry to perform an action....
2
votes
1answer
254 views

Missing javascript .map files

When I view the browser console whilst viewing the Dashboard for instance, I get a whole load of 404s for Javascript .map files. For example on a fresh update to v2.3 I get this lot reported as ...
2
votes
2answers
221 views

Live Preview Not Updating - No errors found

Okay, first I know this question has been asked - specifically here: Live preview not automatically updating But that suggestion did not help. I have no console errors, no network errors. In fact, ...
2
votes
2answers
107 views

How can I get cookies that were set in javascript from a twig template? [NEW]

This question was already asked a few years ago, and the way of doing it was through a plugin. I was wondering if there is a way of doing it just using Craft and Twig. I set up a cookie using ...
2
votes
1answer
415 views

How can I validate a Redactor rich text field in a front-end form using the VeeValidate Vue.js plugin?

Scenario I'm using the VeeValidate Vue.js plugin to validate a Craft front-end form that contains a Redactor rich text field. Problem Validation of the rich text field fails because Redactor adds ...
2
votes
1answer
140 views

Minifying bower_components js files

I am currently using spoeken's gulp workflow (https://github.com/spoeken/gulp-craftcms). Everything works well in development, however, when it comes to production, I can't seem to concat/minify my ...
2
votes
1answer
217 views

Craft 3 js - extend the field layout designer field settings

I would like to have the ability to include a custom option in the field layout designer field settings. There is already a plugin for Craft 2 that uses this feature Relabel for Craft And I would ...
2
votes
1answer
105 views

How to extend Craft.BaseElementIndex for a custom Element Type

I have a custom Element and need to modify the Element Index page. I'm attempting to extend Craft.BaseElementIndex and register the extended class on my Element Index Page. I'm working on a plugin for ...
2
votes
0answers
636 views

Content-Security-Policy Header vs. CSRF Token and login.html

My company would use the current version of the Content-Security-Policy header. We have problems with inline JavaScript and would not use MD5 checksums with the policy. Eg: CSRF Token support ...
2
votes
0answers
148 views

Live Preview not showing correct content when using single page app with JavaScript

So I am creating a website using Craft with JavaScript for making it a single page app. I load my layout which then calls the pages which works well. The only problem is that when viewing pages ...
2
votes
0answers
44 views

JavaScript equivalent to “namespaceInputName”?

Is there a JavaScript equivalent to namespaceInputName? My fieldtype settings are doing some interesting JavaScript gymnastics. Part of the process involves creating an array on the fly (so when the ...
1
vote
2answers
261 views

Can a plugin add JS or CSS to the control panel in Craft 3?

I want entries in a specific PlainText field for a named group to be pre-filled with random content. I am aware of the https://github.com/lukeyouell/craft-randomkeygenerator plugin and am aware that I ...
1
vote
2answers
213 views

Verbb Comments plugin - Can't see comment replies

I recently installed Verbbs Comments plugin on my Craft site, which seems all well and good except that I can't toggle the comment replies. ('2 comments' when clicked does nothing) Everything else ...
1
vote
1answer
562 views

minify javascript with includeJS

If i include javascript in a template using: {% set myJS %} //some JS in here {% endset %} {% includeJS myJS %} Is there a way to minify the javascript that gets output on the page? I had a look at ...
1
vote
1answer
4k views

Get value from dropdown and use in Twig

I’m creating an archive of news article entries. For that purpose, I’m trying to get a value from a dropdown (user-selected year and month) and then assign it to a pagination procedure that calls up ...
1
vote
1answer
213 views

Craft 3 - custom callback after Garnish onDragStop

I would like to trigger a custom javascript function after an element in a matrix block was moved in the matrix but I'm not sure how to attach the event without changing the MatrixInput.js. So far I'...
1
vote
2answers
288 views

Update Commerce Cart using Fetch

Hi this is a variation of the question asked here on updating the cart using ajax, instead I'm attempting to use the Fetch API. As a test the form submits properly when submitting in the usual way ...
1
vote
1answer
1k views

Ajax Function on a search form not working in Safari & IE

I made an ajax function so my page wont refresh and my search window wont disappear on form submit. It works fine in all browsers but it keeps refreshing on Safari. Here is my code: {% if not craft....
1
vote
1answer
615 views

How reference “Craft” js object in jquery scope

I have this function within my <script> tags: $('.btn').click(function() { var data = { title: "This is a title" }; Craft.postActionRequest('plugin/controller/action', data,...
1
vote
1answer
77 views

includeJs not consistently working

Using Craft 2 and I have a Matrix Block which can contain a carousel item, which uses JS/jQuery. Because the content editor can add multiple instances of this Matrix Block I can’t set up these ...
1
vote
1answer
81 views

Plugin field-type not getting javascript variable?

Updating a plugin from Craft 2 to Craft 3, so this logic used to work. The plugin has a field-type which is a button. When clicked, the button does stuff depending upon another field. That handle(ID) ...