How do I get the href of an element and also assign it as the id of the element without the sharp (#)?

0

I'm using a shortcode in Wordpress that only gives me the ability to enter the following attributes:

<a href="#anatomia" target="_self" class="dt-sc-button small">Inscreva</a>
<a href="#anatomia" target="_self" class="dt-sc-button small">Saiba mais</a>

I would then like to get the href attribute and insert it as the element ID. Since I can have more than one element (7 to start), I thought I'd put it inside a foreach to repeat that for all the elements.

Another thing ... I thought about using the dt-sc-button class to restrict the application to these elements.

    
asked by anonymous 01.08.2015 / 21:13

1 answer

1

If you have a class for these elements you can use $('.dt-sc-button') and then iterate them with a .each() .

To get href you can use $(this).attr('href') within that .each() and then just need to get the first character. .slice(1) does this.

Code hint:

$('.dt-sc-button').each(function () {
    this.id = $(this).attr('href').slice(1);
});
    
01.08.2015 / 22:25