Note that there are some explanatory texts on larger screens.

plurals
  1. POFirst jQuery plugin issues
    primarykey
    data
    text
    <p>Hey guys, first off let me just say Im a jQuery noob! I want to make a simple plugin that auto-populates a html select element, it works fine on the first element but when I call it again to populate a second it appends nothing. Here are my calls in the ajax tab where #product and #new-category are the select elements:</p> <pre><code>$(function(){ $("#product").popSelect("products"); $("#new-category").popSelect("categories"); }); </code></pre> <p>HTML:</p> <pre><code>&gt;&lt;select id="product" name="product"&gt; &gt; &lt; option value=""&gt;Select Product&lt;/option&gt; &gt;&lt;/select &gt; &gt;&lt;select id="new-category" name="new-category"&gt; &gt; &lt; option value=""&gt;Select Category&lt; /option&gt; &gt;&lt;/select &gt; </code></pre> <p>And here is the Plugin:</p> <pre><code>(function(jQuery){ jQuery.fn.popSelect = function(table) { return jQuery(this).each(function(){ var obj = jQuery(this); if(this.nodeName.toLowerCase() == 'select'){ jQuery.getJSON("../app/modules/ajax/json.php", { table:table }, function(data){ var options = ''; jQuery.each(data, function(i,item){ options += '&lt;option value="' + item.id + '"&gt;' + item.title + '&lt;/option&gt;'; }); obj.append(options); }); }; }); }; })(jQuery); </code></pre> <p>Strangely if I change the second function call <code>$("#new-category").popSelect("categories");</code> to <code>$("[id^='new-category']").popSelect("categories");</code> it then works fine, is there something wrong with my selector?</p> <p>Thanks for any help!</p>
    singulars
    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