How do I remove an attribute with an invalid name with jQuery

I need to remove an attribute with an invalid name from a link that I don’t have access to at the moment, but still need to change.

Until I can get to the code I am using jQuery to change the element’s href attribute and remove some unnecessary attributes:

jQuery(“.description p a”).each(function() {
jQuery(this).attr(“href”, “https://newsite.com/pricelist”);
jQuery(this).attr(“title”, “Have a look at our new pricelist”);
jQuery(this).removeAttr(“attr1 attr2 attr3”); //eight attribute in total
});

This works as exptected.

However, the a element also contains a ninth (empty) attribute with an invalid name, namely n' (including the semi-colon). I don’t know how it got there, but I need to get rid of it.

When I add n' to the list of removable attributes in the above code the browser console states Uncaught DOMException: Failed to execute ‘setAttribute’ on ‘Element’: ‘n'’ is not a valid attribute name, which I understand. However, when I escape the &, # and ; characters (n') the console shows the exact same error.

I know that ' is the HTML-code representing the quote character (‘). I have dabbled with html_entity_decode, but can’t seem to get it right.

How do I get rid of this invalidly named attribute using jQuery?

Read more here:: How do I remove an attribute with an invalid name with jQuery

Leave a Reply

Your email address will not be published. Required fields are marked *