Search code examples
phpjquerywordpresshtml-heading

Automatic id on tags


Hello i'm building a side with a local scroller. Is there any way i can take the value of my tag and use that as id.

<h2>test</h2>

so it would be like this

<h2 id="test">test</h2>

I'm running wordpress.


Solution

  • $('h2').each(function(){
       this.id = $(this).text();
    });
    

    But beware that not all characters are allowed in an id :

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").