Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery Parent Child Selector Improvement
    primarykey
    data
    text
    <p>I am learning Jquery and hope to do things better. I have found a solution, and want to see how a seasoned veteran would do it... If there is more eloquence, so to speak.</p> <p>The problem is I have a widget that rests in a table row. The widget has the same classes as all the other 20 widget. When someone clicks on a particular widget, only that widget should change.</p> <p>Here's the HTML:</p> <pre><code>&lt;div class="deal-widget"&gt; &lt;div class="deal-widget-left"&gt; &lt;div class="deal-widget-left-countdown"&gt; &lt;span class="totalcoupons"&gt;500&lt;/span&gt; of &lt;span class="maxcoupons"&gt;500&lt;/span&gt; &lt;/div&gt; &lt;div class="deal-widget-left-remain"&gt; Remaining &lt;/div&gt; &lt;/div&gt; &lt;div class="deal-widget-right is_open"&gt; &lt;a href="/deal/claim/1113" class="deal-link"&gt;Claim It!&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Here's my stab at the JQuery:</p> <pre><code>Drupal.behaviors.plusone = function (context) { $('a.deal-link:not(.deal-processed)', context).click(function () { var parent = $(this).parents('div.deal-widget'); var originalVote = parent.children('div.deal-widget-left').children('div.deal-widget-left-countdown').children('span.totalcoupons').text(); var originalVoteInt = parseInt(originalVote); var voteSaved = function (data) { parent.children('div.deal-widget-left').children('div.deal-widget-left-countdown').children('span.totalcoupons').html(originalVoteInt - data.total_votes); parent.children('div.deal-widget-right').html(data.voted); parent.children('div.deal-widget-right').removeClass('is_open').addClass(data.votedClass); } $.ajax({ type: 'POST', url: this.href, dataType: 'json', success: voteSaved, data: 'js=1' }); return false; }) .addClass('deal-processed'); } </code></pre> <p>Is there a better way of selecting parent then second set of children to a clicked-upon object than this?</p> <p>Thanks</p> <p>Joe</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.
 

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