Question

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.

Was it helpful?

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 (".").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top