A user wanted to be able to display a serial number associated with some of the options in a large select drop down element. The request was to display the number if there was one, otherwise to hide the field. While this is an uncommon request the code is posted here because it could be adapted to meet various similar needs.
The options for the select drop-down were created using a Custom Code element with the serial number in a data-serial attribute like this
<select name='product_list' id='product_list' . . . > <option id="hide_none3" value="BRILLIANT SPA® - #0361" data-serial='12345' > . . . </option> . . . </select>
Using a data- . . . attribute allows us to add data like the serial number to the form and stay compliant with HTML specifications so that the form will still validate if needed.
window.addEvent('domready', function() { $('serial_no_container_div').hide(); $('product_list').addEvent('change', function() { var opt; opt = this.getSelected().getProperty('data-serial').toString(); if ( opt.length > 0 ) { $('serial_no').value = opt; $('serial_no_container_div').show(); } else { $('serial_no').value = ''; $('serial_no_container_div').hide(); } }); });
ChronoForms v5 code
This is the equivalent JavaScript re-written using jQuery for use with ChronoForms v5 on Joomla! 3.
jQuery(document).ready(function (jQ) { jQ('#form-row-serial_no').hide(); jQ('#product_list').change(function () { var opt; opt = jQ(this).find(':selected').data('serial').toString(); if (opt.length > 0) { jQ('#serial_no').val(opt); jQ('#form-row-serial_no').show(); } else { jQ('#serial_no').val(''); jQ('#form-row-serial_no').hide(); } }); });
Comments: