• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Fetch from ID and add as title for SVG

meetdilip

Well-Known Coder
The below code is from an SVG symbol file. It now has <title> which was not there before. I manually added it by referring to " class ". In this case, it is GBP. The title helps us search inside the SVG sprite/symbol file. I have a file with around 1300 + SVG symbols. This means there are 1300 icons without a title but has the info as " class " for each of them.

Is it possible to fetch the info from " class " and add a <title></title> for each icon ?

[CODE lang="xml" title="SVG Symbol"]<symbol

id="symbol16060"
width="16"
height="16"
fill="currentColor"
class="bi bi-currency-pound"
viewBox="0 0 16 16">

<title id="pound gbp">GBP Currency Pound</title>

<path

d="M4 8.585h1.969c.115.465.186.939.186 1.43 0 1.385-.736 2.496-2.075 2.771V14H12v-1.24H6.492v-.129c.825-.525 1.135-1.446 1.135-2.694 0-.465-.07-.913-.168-1.352h3.29v-.972H7.22c-.186-.723-.372-1.455-.372-2.247 0-1.274 1.047-2.066 2.58-2.066a5.32 5.32 0 0 1 2.103.465V2.456A5.629 5.629 0 0 0 9.348 2C6.865 2 5.322 3.291 5.322 5.366c0 .775.195 1.515.399 2.247H4v.972z"
id="path1872-3" />

</symbol>[/CODE]


Can this code help ? If no, it would be nice to have some help. Thanks

[CODE lang="javascript" title="class to title"]
var className = document.getElementsByTagName('symbol')[0].getAttribute('class')


document.getElementsByTagName('symbol')[0].setAttribute('Title', className)


console.log(document.getElementsByTagName('svg')[0])[/CODE]
 
Top Bottom