Skip to content
Advertisement

JS replace all matching id values

A website has multiple pages with imported HTML from another page with id tags that need to be simplified.

It currently looks like this.

JavaScript

There are anchor links that point to all of these ids, but these links do not include the “user-content-” part. They look like this Link to anchor. They do NOT look like this Link to anchor. There are too many of these id’s to change manually.

How can I change the value of all the id tags from id="user-content-test1 to just id="test1 using jQuery or pure JS? The desired result should be:

JavaScript

I have searched all over stackoverflow and google but I only find how to replace strings, not IDs. I have tried both of these scripts with no results.

JavaScript
JavaScript

Advertisement

Answer

use the selector $("h2 a[id^=user-content]") -> that means select all id beginning by user-content

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement