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

192
Views
Is there a way to 'reset' a jQuery function each time it is triggered?

I have an eCommerce website product page for a product with color options.

I'm trying to copy the selected color attribute value and append it to the product title, and I'm using any change to the value of #pa_finish as a trigger, to ensure the title is updated with the current selection.

Here is the code I'm presently using:

        $( '#pa_finish' ).change(function(){         
                
           var var_name = $('#pa_finish :selected').text();
           var original = $('h1.product_title.entry-title').text();
            
           $('h1.product_title.entry-title').html(original + ' ' + var_name);

        });

The issue I'm having is that, instead of the title refreshing with each trigger, the var_name variable is being iterated alongside the previous value, so the product title ends up simply increasing in length!

Ideally, the pre-existing text value from var_name is cleared, and replaced with the new selection each time a change is made.

Is there a means by which I can 'reset' the function each time it is triggered? Thank you in advance for any help or insight anyone might be able to provide.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thank you all for your input - I've realised that, as pointed out in the comments, I hadn't established a baseline value for my title.

I have a working example now:

var title = $('h1.product_title.entry-title').text();
  
$( '#pa_finish' ).change(function() {
    var var_name = $('#pa_finish :selected').text();
            
    $('h1.product_title.entry-title').html(title + ' ' + var_name);
});

I've established a value for the original title in a 'title' variable (outside of the function), so that when the function executes it replaces the HTML of:

$('h1.product_title.entry-title')

...with a combination of the original title and the var_name variable, on each change.

Before, it was simply loading the selector again and again, and tagging on the result of var_name, which is why I saw the repitition.

I hope this can help someone in the future.

about 4 years ago · Juan Pablo Isaza Report
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!