Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How to triger button onclick on a tag

I have one anchor tag and a button inside the same div parent.

Clicking on an anchor need to click the button too via code.

Below is my HTML

<div class="product_list">
    <a class="action tocart primary new_add_to_cart"><span>Add to cart</span></a>
    <button type="submit" id="product-addtocart-button-927" title="Add to Cart" class="main-add-tocart action tocart primary">
        <span>Add to cart</span>
    </button>
</div>
<div class="product_list">
    <a class="action tocart primary new_add_to_cart"><span>Add to cart</span></a>
    <button type="submit" id="product-addtocart-button-928" title="Add to Cart" class="main-add-tocart action tocart primary">
        <span>Add to cart</span>
    </button>
</div>

Below is the JavaScript code I tried to use

$(document).on('click','.new_add_to_cart',function(){
    var pin = $.cookie('delivery-pincode');
    if(!pin){
        alert("hii2");
    }
    else{
        $(this).closest(".main-add-tocart").trigger("click");
    }
});

In the else condition I want the closest button (sibling) to be clicked, I have multiple product(parent divs with the same class .product_list) and I want only click only on the "current" product.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!