0

I have the following button link:

<a href="e5f6ad6ce374177eef023bf5d0c018b6" data-id="573" onclick="reEnableBtn(573)">×</a>

And the following js:

function reEnableBtn(prodId) {

   alert(prodId);
};

Until now all good, on click of the link I get an alert with the content: 573 as expected.

Next thing I want to do is with the following html:

<a href="/new-page/?add-to-cart=573" class="button product_type_simple add_to_cart_button ajax_add_to_cart added" data-product_id="573">
<i class="fa fa-shopping-cart finished disabled-button"></i>
</a>

Within the JS I want to add a removeClass function which removes the classes 'finished' and 'disabled-button' from the <i> where the data-product_id of the parent <a> matches the value of prodId within the JS function (because I have other links with same structure but different data-product_id).

How do I do this?

2
  • What code have you written for the second part of your question so far? It seems completely unrelated to the first Commented Apr 20, 2018 at 8:55
  • 1
    You can use jquery function .removeClass("classname") to remove any class Commented Apr 20, 2018 at 8:57

3 Answers 3

1

You can

1) use attribute equal selector to target anchor element with same data product id

2) find element i in above anchor element

3) use removeClass to remove multiple classes from it

function reEnableBtn(prodId) {
   $('[data-product_id=' + prodId + '] i').removeClass("finished disabled-button");
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it's working :) Can you please tell how can i add just a small delay to remove this class?
what is the expected behavior after delaying removeClass?
after 2 seconds will remove the disabled-button class.
You can use setTimeout
0
function reEnableBtn(prodId) {
 $("a[data-product_id='"+prodId+"']").find("i").removeClass("finished disabled-button");
};

Use this simple script.

Comments

0

Use this code

jQuery(function($){

    var s = $("body").find('[data-product_id=573]');

    s.on("click", function(e){
        var i = $(this).find('i');
        if (i.hasClass('finished'))
            i.removeClass('finished');
        if (i.hasClass('disabled-button'))
            i.removeClass('disabled-button');
        alert("The classes was removed.");
        return false; // this is for disabling anchor tag href
    });
})

jsfiddle

If you have any other question feel free to ask 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.