Note that there are some explanatory texts on larger screens.

plurals
  1. POIf element has class that has been dynamically added then do
    primarykey
    data
    text
    <p>I have an element which has a class added through a function which I would like to do a check on.. </p> <p>If ELEMENT has class then show another element</p> <p>I've got an if else statement which I thought would work but class="selected" seems to only work when the page isnt dynamically changing the class.</p> <p>Im using jquery isotope as a filtering tool, which is adding the selected class if that helps.</p> <pre><code>if ($("#comingsoonfilter").hasClass('selected')){ //this gets dynamically added $("#datesorter").css({ 'display' : 'block' }); } else { $("#datesorter").css({ 'display' : 'none' }); } </code></pre> <p>HTML</p> <pre><code>&lt;div class="filter big"&gt; &lt;p&gt;Filter Schemes:&lt;/p&gt; &lt;ul id="filters" class="option-set clearfix" data-option-key="filter"&gt; &lt;li&gt;&lt;a href="#filter" data-option-value="*" class="selected"&gt;All Schemes&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#filter" data-option-value=".comingsoon" id="comingsoonfilter"&gt;Coming Soon&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div class="filter"&gt; &lt;p&gt;View by:&lt;/p&gt; &lt;ul id="sort-by" class="option-set clearfix" data-option-key="sortBy"&gt; &lt;li&gt;&lt;a href="#sortBy=original-order" data-option-value="original-order" class="selected" data&gt;Date Added&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#sortBy=name" data-option-value="name"&gt;Alphabetical&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#sortBy=price" data-option-value="price"&gt;Price&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#sortBy=date" data-option-value="date" id="datesorter"&gt;Date&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p></p> <p>ANSWER :</p> <p>Had to fire the function in the same part as the bit that was adding the .selected class the whole part of javascript for this part (including the isotope is below), if anyone gets stuck.</p> <pre><code>$(function() { var $container = $('.scheme-archive'); $checkboxes = $('#cityfilters input'); $container.isotope({ itemSelector : '.scheme', getSortData : { price : function( $elem ) { return parseFloat( $elem.find('.price').text().replace( /[\£\,]/g, ''), 1000 ); }, date : function( $elem ) { return $elem.find('.date').text(); }, name : function ( $elem ) { return $elem.find('.name').text(); } } }); $checkboxes.change(function() { var filters = []; // get checked checkboxes values $checkboxes.filter(':checked').each(function(){ filters.push( this.value ); }); filters = filters.join(', '); $container.isotope({ filter: filters }); }); var $optionSets = $('#options .option-set'), $optionLinks = $optionSets.find('a'); $optionLinks.click(function() { var $this = $(this); // don't proceed if already selected if ( $this.hasClass('selected') ) { return false; } var $optionSet = $this.parents('.option-set'); $optionSet.find('.selected').removeClass('selected'); $this.addClass('selected'); // make option object dynamically, i.e. { filter: '.my-filter-class' } // Show / hide the date sorter if ($('.selected').is('#comingsoonfilter')){ $('#datesorter').show(); } else { $('#datesorter').hide(); } var options = {}, key = $optionSet.attr('data-option-key'), value = $this.attr('data-option-value'); // parse 'false' as false boolean value = value === 'false' ? false : value; options[ key ] = value; if ( key === 'layoutMode' &amp;&amp; typeof changeLayoutMode === 'function' ) { // changes in layout modes need extra logic changeLayoutMode( $this, options ); } else { // otherwise, apply new options $container.isotope( options ); } return false; }); }); </code></pre>
    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.
    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