I have made a module that in the backend, the user selected from 2 dropdown multi select lists, some names and photos and in the frontend I print the selected names/photos.
To make it easier for user to track how many names he selected, i added a text box at the backend that i want it to keep the count ( with jquery ) of how many options the user selected. I wrote the script and it is working fine ( http://jsfiddle.net/u9dvmytn/10/ ).
But when i try to run it , inside my module jquery seems to not run at all ( consol.log is empty ). From what i can see, i did something wrong on " how to include the jquery library " or ( more likely, i really messed up on how to add/call the script inside the custom field ). I cant find anything at all , about how to properly add jquery inside custom fields and joomla backend at google,so could someone actualy help me to findout how i actually add a jquery script to backend, and what i did wrong in my module?
The js.js Script
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript">
</script>
<script type="text/javascript">
jQuery(document).ready(function($){
var count =$("#\\#jform_params_foreignmanuf :selected").length;
$("#\\#jform_params_manucounter").val(count);
});
</script>
The multiselect dropdown list field type
<?php
defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('list');
class JFormFieldManulist extends JFormFieldList
{
protected $type = 'Manulist';
protected function getOptions()
{
JHtml::_('jquery.framework');
$db= JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__virtuemart_manufacturers_en_gb');
$db->setQuery($query);
$items = $db->loadObjectList();
$options = array();
if ($items)
{
foreach($items as $item)
{
$options[] = JHtml::_('select.option', $item->virtuemart_manufacturer_id , $item->mf_name,$item->slug);
}
}
$options = array_merge(parent::getOptions(), $options);
**JHtml::_('script', JUri::root() . 'modules/mod_manufacturers/js/js.js');**
return $options;
}
}
The Text box field type that will keep the count (i dont return something here. maybe is that the problem?)
<?php
defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('Text');
class JFormFieldCountermanu extends JFormFieldText
{
protected $type = 'Countermanu';
protected function getOptions()
{
JHtml::_('jquery.framework');
$doc = JDocument::getDocument();
$js = '
jQuery(document).ready(function($){
var count =$("#\\#jform_params_foreignmanuf :selected").length;
$("#\\#jform_params_manucounter").val(count);
});
';
JHtml::_('script', JUri::root() . 'modules/mod_manufacturers/js/js.js');
$doc->addScriptDeclaration($js);
}
}
#
to your element id's. So replaceid="#jform_params_foreignmanuf"
withid="jform_params_foreignmanuf"
and same goes for the input – Lodder Dec 9 '14 at 11:39