1

I'm a newbie when it comes to jQuery and I'm hoping if there's a way to override an existing on click function.

The code is as follows:

$('.woo-star-rating').on('click', function() {
    $('.woo-star-rating-filled').width( $(this).attr('data-rating') * 25 )
});

and I'd like to change the number 25 to 21 inside my own .js file.

$('.woo-star-rating').on('click', function() {
    $('.woo-star-rating-filled').width( $(this).attr('data-rating') * 21 )
});
2
  • What are you asking? Simply change 25 to 21 and save the js file Commented Sep 25, 2016 at 20:50
  • Seems like I didn't explain it properly. The first code is part of a plugin and therefore I can't modify the original code. Commented Sep 25, 2016 at 21:00

1 Answer 1

4

You can remove the existing click event handler using the .off() method add your new click event.

var $rating = $('.woo-star-rating');

$rating.off('click');
$rating.on('click', function() {
    $('.woo-star-rating-filled').width( $(this).attr('data-rating') * 21 )
});

This can be chained into:

$('.woo-star-rating').off('click').on('click', function() {
    $('.woo-star-rating-filled').width( $(this).attr('data-rating') * 21 )
});

Here's a working example:

// add junk event
$('#test-button').on('click', function() {
  console.log('first button click');
});

// remove first event
$('#test-button').off('click');

// add new event, only this will fire on click
$('#test-button').on('click', function() {
  console.log('second button click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="test-button">Test Button</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.