5

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 cannot tell if that is possible with the elementSelector macro. I know the element select modals can be extended using Craft.createElementSelectorModal(). Is this the only way to access element select modal callbacks?

6

You can get the Element Select’s JS instance like so:

$(document).ready(function() {
  var elementSelect = $('#your-container-id').data('elementSelect');
});

(Replace your-container-id with whatever ID you passed into the id param when calling forms.elementSelect or forms.elementSelectField.)

Then you can attach a selection callback to that:

elementSelect.on('selectElements', function(elements) {
  // ...
});

There’s also a 'removeElements' callback function you can add.

  • 1
    Perfect! This is exactly what I was looking for. – kcolls Oct 20 '15 at 20:10
  • 1
    Hi Brandon, I've brought my plugin into another project, and it seems like the element select isn't binding to my container. When I log $('#my-container-id').data(), the element select is not there. I can use my element select field and select an asset, but there is no element select on my container. I've double checked all names being passed, and I'm unsure of what might cause this, or how to debug it. Do you have any thoughts? – kcolls Jan 11 '16 at 21:01
  • 1
    Oo nevermind. I realized that this was added in 2.4.2688, and the site I am working on is below that. – kcolls Jan 11 '16 at 23:19

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.