Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery add count to class
    primarykey
    data
    text
    <p>I've been stairing, trying and tweaking this code for over 2hrs now and I'm still no closer to figuring it out. </p> <p>I'm trying to do the following,</p> <p>If div has class <strong>.item1</strong>, <strong>.item2</strong>, <strong>.item3</strong> add css style to each, But I need the .item(number) to get count automatically as further in my script I have divs that are automatically created with .item(number) classes.</p> <p>I just need the '.single-attendee-wrapper' to have '.item + (i+1)' aswell but I cant get this to work.</p> <p>Latest amends - </p> <pre><code>/* Inline validation */ .on( 'blur change', '.input-text', function() { $(".single-attendee-wrapper").each(function(i){ var $this = $(this); var validated = true; if ( $(".cart &gt; .item" + (i+1)) ) { if ( $this.val() == '' ) { $(".item" + (i+1) + " .count").css('background','#ED616A'); $(".item" + (i+1) + " .count").css('color','#fff'); validated = false; } } if ( validated ) { $(".item" + (i+1) + " .count").css('background','#D1D3D4'); $(".item" + (i+1) + " .count").css('color','#808285'); } }); } ) </code></pre> <p>Here is my snippet so far,</p> <pre><code>.on( 'blur change', '.input-text', function() { var count = 0; $(".single-attendee-wrapper .input-text").each(function(){ var $this = $(this); var validated = true; count++; if ( $(this) ) { if ( $this.val() == '' ) { $('.item'+ count + ' .count').css('background','#ED616A'); $('.item'+ count + ' .count').css('color','#fff'); validated = false; } } if ( validated ) { $('.item'+ count + ' .count').css('background','#D1D3D4'); $('.item'+ count + ' .count').css('color','#808285'); } }); } ) </code></pre> <p>Further into my script, this works perfect.</p> <pre><code>$(".single-attendee-wrapper").each(function(i) { $(this).addClass("item" + (i+1)); }); </code></pre> <p>HTML</p> <pre><code>&lt;div class="single-attendee-wrapper item1"&gt; &lt;div class=" product-addon product-addon-one-attendee" style="display: block;"&gt; &lt;div class="count" style=""&gt;1&lt;/div&gt; &lt;/div&gt; &lt;div class="single-attendee-wrapper item2"&gt; &lt;div class=" product-addon product-addon-two-attendees" style="display: block;"&gt; &lt;div class="count" style=""&gt;2&lt;/div&gt; &lt;/div&gt; &lt;div class="single-attendee-wrapper item3"&gt; &lt;div class=" product-addon product-addon-three-attendees" style="display: block;"&gt; &lt;div class="count" style=""&gt;3&lt;/div&gt; &lt;/div&gt; </code></pre>
    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.
 

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