Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:08:15+00:00 2026-05-23T16:08:15+00:00

I am learning Jquery and hope to do things better. I have found a

  • 0

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.

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.

Here’s the HTML:

<div class="deal-widget">
  <div class="deal-widget-left">
    <div class="deal-widget-left-countdown">
      <span class="totalcoupons">500</span> of <span class="maxcoupons">500</span>
    </div>
    <div class="deal-widget-left-remain">
      Remaining
    </div>
  </div>
  <div class="deal-widget-right is_open">
    <a href="/deal/claim/1113" class="deal-link">Claim It!</a>
  </div>
</div>

Here’s my stab at the JQuery:

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');
}

Is there a better way of selecting parent then second set of children to a clicked-upon object than this?

Thanks

Joe

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:08:16+00:00Added an answer on May 23, 2026 at 4:08 pm

    I’d suggest using:

    var parent = $(this).closest('div.deal-widget');
    var originalVote = parent.find('span.totalcoupons').text();
    

    closest() searches the ancestor elements for the first element that matches the provided selector; find() searches the descendent elements for those that match the provided selector.

    While closest() returns only one match (the closest/*first* element that matches the selector) it’s important to note that find() may return multiple elements.


    References:

    • closest().
    • find().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Learning jQuery and Getting Better at Javascript I have found jQuery very
I'm learning JQuery and have found the factory function $() with its selectors quite
i found http://visualjquery.com/ more useful when i was learning jQuery with each function explained
learning Jquery and integrating with PHP - getting there, but have one last challenge
I am learning JQuery, and I have ran into an odd issue. I made
I have just started learning Jquery and am new to writing javascript (I am
Beyond the official documentation, are there any recommended resources for learning to build jQuery
Learning jquery, so please be kind :) Using PHP, I have a table with
I'm learning jQuery/javascript and have a rather basic question. Why doesn't this work? Thanks
I am a novice learning jQuery and I have a scenario; I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.