<p>You can get the Element Select’s JS instance like so:</p>

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

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

<p>Then you can attach a selection callback to that:</p>

<pre><code>elementSelect.on('selectElements', function(elements) {
  // ...
});
</code></pre>

<p>There’s also a <code>'removeElements'</code> callback function you can add.</p>

CC BY-SA 3.0
2015-10-20T20:10:33.923Z
Perfect! This is exactly what I was looking for.
kcolls
CC BY-SA 3.0
2016-01-11T21:01:27.557Z
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
CC BY-SA 3.0
2016-01-11T23:19:19.333Z
Oo nevermind. I realized that this was added in 2.4.2688, and the site I am working on is below that.
kcolls
CC BY-SA 3.0
2015-10-19T22:33:41.667Z
2015-10-19T22:33:41.667Z
Brandon Kelly
<p>I am writing a custom field type that makes use of Craft's asset selector, and saw that there is an elementSelector macro (in <code>includes/_forms</code>). 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 <code>Craft.createElementSelectorModal()</code>. Is this the only way to access element select modal callbacks?</p>

<plugin-development><assets><custom-fieldtype><javascript><modal>
Are there callbacks for elementSelect macro?
# Are there callbacks for elementSelect macro?

(Asked by: username_0 on 2015-10-19)

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?

**Tags:** plugin-development, assets, custom-fieldtype, javascript, modal


---

## 1 answers

### Answer by username_1 on 2015-10-19. Score: 6 | Accepted answer

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.

> **Comments:**
> **username_0**: Perfect! This is exactly what I was looking for.  
> **username_0**: 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?  
> **username_0**: Oo nevermind. I realized that this was added in 2.4.2688, and the site I am working on is below that.  

---

