I am using jQuery replaceWith() method to replace some content from the html. Its working fine. But I want to change the data attribute value once replaceWith() has been done.
So basically I am getting this from my ajax response
UpdatedItem = '<li data-cart-key="XXX" data-item-name="Test" data-item-price="$111.00" data-item-key="XXX"><span class="cart-action-wrap"><a class="edit-cart-item" data-cart-item="XXX" data-cart-key="XXX" data-cart-action="edit">Edit</a><a class="remove-cart-item" data-cart-item="XXX" data-cart-key="XXX" data-cart-action="remove">Remove</a></span></li>';
Now I am using replaceWith() here like this
$( 'ul.custom-contents' ).find( 'li.updated' ).replaceWith( UpdatedItem );
Here I wanted to use methods like update data attribute value, remove class, add class after replaceWith() has been done.
So can someone tell me is there any way available to do this? Any help and suggestions would be really appreciable. Thanks
2
Answers
You can create a jQuery object inside
replaceWith
, do the operations and return it. Use.find()
for nested elements:you can solve like this.