Note that there are some explanatory texts on larger screens.

plurals
  1. POString variable passed from a function to a function name does not work
    primarykey
    data
    text
    <p>I am trying to use the value of a drop list as the the name of the next function. The string is correct and shows in an alert. Also writing the name explicitly in the code does work. But using parts from the variable in the scope of both functions does not work at all.</p> <pre><code> Drupal.behaviors.smart_inventory = { attach: function(context, settings) { // this should be in the scope of both functions var selecttype; $('#select-voc-content_types', context).change(function () { contenttype = $( this ).val(); secondary = $('#' + contenttype); if($(secondary).length &gt; 0) { // set the select list. tried an alert and the variable string is set selecttype = '#select-voc-' + $( this ).val(); $('.group-types').show(); $('#' + contenttype).show(); $('#object-ajax-form').hide(); } else { $('#object-node-form-message').show(); $.post('smart_inventory/ajax', { "node-type": contenttype }, frmDrupal); } }); // this does not respond as jquery does not accept the string as an element name // or maybe the variable is not available here? $( selecttype, context ).change(function () { var nodetype = $( this ).val(); $('#object-node-form-message').show(); $.post('smart_inventory/ajax', { "node-type": nodetype }, frmDrupal); }); var frmDrupal = function(responseText) { $('#object-ajax-form').show(); $('#object-ajax-form').html(responseText); $('#object-node-form-message').hide(); } } </code></pre> <p>};</p> <p>If found that this works! But is nesting a function good practice? or good enough? ;</p> <pre><code> Drupal.behaviors.smart_inventory = { attach: function(context, settings) { var selecttype; $('#select-voc-content_types', context).change(function (selecttype) { contenttype = $( this ).val(); secondary = $('#' + contenttype); if($(secondary).length &gt; 0) { // set the select list selecttype = '#select-voc-' + $( this ).val(); $('.group-types').show(); $('#' + contenttype).show(); $('#object-ajax-form').hide(); } else { $('#object-node-form-message').show(); $.post('smart_inventory/ajax', { "node-type": contenttype }, frmDrupal); } $( selecttype , context ).change(function () { var nodetype = $( this ).val(); $('#object-node-form-message').show(); $.post('smart_inventory/ajax', { "node-type": nodetype }, frmDrupal); }); </code></pre> <p>});</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload