Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the same JQuery tokeninput box multiple times on a page
    primarykey
    data
    text
    <p>I am using Jquery TokenInput in my rails app to help users quickly input information. Each user has a profile with many items of the same class on it, call them college_items. Each college_item has its own edit link, which pops up a modal with the appropriate edit form for the modal. </p> <p>I followed Ryan Bates's screencast and got TokenInput working properly using the following code: <br />In application.js:</p> <pre><code>$(function() { $("#education_major_tokens").tokenInput("/majors.json", { theme: "facebook" }) }); </code></pre> <p>In my view:</p> <pre><code>&lt;%= f.text_field :major_tokens %&gt; </code></pre> <p>In my model:</p> <pre><code>def major_tokens=(ids) self.major_ids = ids.split(",") end </code></pre> <p><br />The problem I'm running into is that this same field appears in each edit form, so in the edit form for the first item everything works perfectly, but in the edit form for any other item the javascript is not initialized. My question is how can I change the javascript to initialize each time this form element appears, not just the first?</p> <p>EDIT: I thought something like this might be the solution: </p> <pre><code>$(function() { $("#education_major_tokens").each(function(){ $(this).tokenInput("/majors.json", { theme: "facebook" }) }); }); </code></pre> <p>but it still isn't working for me. Anyone out there have any ideas?</p> <p>EDIT 2: Figured this out finally. See my edit above, only instead of using #education_major_tokens, I set each input to have :class => "major_input" and changed "#education_major_tokens" in the code to ".major_input"</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.
    1. This table or related slice is empty.
    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