Note that there are some explanatory texts on larger screens.

plurals
  1. PONot storing in the array
    primarykey
    data
    text
    <p>On my page I have:</p> <pre><code> function grabProfiles(searchstring) { $.post('newsletter.php?mode=grab', { search: searchstring }, function(data) { $('#newsletter_receivers').html(data); }); } $('#newsletter_searchprofiles').bind('keyup', function() { var searchstring = this.value; grabProfiles(searchstring); }); $(document).ready(function() { grabProfiles(''); }); </code></pre> <p>This outputs the result in div element #newsletter_receivers. The result looks something like this:</p> <pre><code>&lt;div class="receiver"&gt; Peter &lt;/div&gt; &lt;div class="receiver"&gt; Maria &lt;/div&gt; &lt;script&gt; $(".receiver").click(function () { $(this).toggleClass("newsletter_receiver_highlight"); }); &lt;/script&gt; </code></pre> <p>So when you click on one of the receivers, they will be highlighted. This works fine.</p> <p>Now I would like to put the receiver's id attribute value (the highlighteds id value) in an array. And then alert this arrays value when you click on a submit button that exists under the #newsletters_receivers. (not in the ?mode=grab where all the .receiver elements is)</p> <p>I did like this:</p> <pre><code> $('#newsletter_send').click(function(){ alert($('div.newsletter_receiver_highlight').length+' Selected'); var arr = []; $('div.newsletter_receiver_highlight').each(function(){ arr.push(this.id); }); alert(arr); }); </code></pre> <p>This gives me two alerts. The one is right, it gives me the numbers of how many is highlighted. The second alert which should give me the values, is empty!</p> <p>This worked previously when I did not made a ajax request to output the receiver elements. And i think it is empty because first time the page doesnt contain anything in #newsletters_receivers , until it makes the ajax request. But how can it tell the right length then?</p> <p>How can i fix/ make a solution for this in order to work?</p> <p>// Conclusion question:</p> <p><strong>Why is each() not live? and alternatives than this method?</strong></p> <p>example:</p> <pre><code>$('div.newsletter_receiver_highlight').each(function(){ arr.push(this.id); }); alert(arr); </code></pre> <p>this stores value for all the CURRENT elements with the class newsletter_receiver_highlight</p> <p>If i append a div element with class newsletter_receiver_highlight later from an request response, it wont store to the array</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