usethe23
usethe23

Reputation: 155

Append href begining based on url id

I'm trying to append the beginning of a list of emails.

this is an example of an email html output :

<a id="skypemail" href="[email protected]">test</a>
<a id="skypemail" href="[email protected]">test</a>
<a id="skypemail" href="[email protected]">test</a>
<a id="skypemail" href="[email protected]">test</a>

And i want it to be :

<a id="skypemail" href="sip:[email protected]">test</a>
<a id="skypemail" href="sip:[email protected]">test</a>
<a id="skypemail" href="sip:[email protected]">test</a>
<a id="skypemail" href="sip:[email protected]">test</a>

Can i do that with JavaScript based on the id="skypemail"?

Upvotes: 1

Views: 51

Answers (3)

Mart&#237;n
Mart&#237;n

Reputation: 3125

If you want to edit multiple elements use the class attribute. ID is just for unique elements, so change id to class:

<a class="skypemail" href="[email protected]">test</a>
<a class="skypemail" href="[email protected]">test</a>
<a class="skypemail" href="[email protected]">test</a>
<a class="skypemail" href="[email protected]">test</a>

And then, run this javascript code:

var emails = document.getElementsByClassName('skypemail');

Array.from(emails).forEach(function(v){
  var elemVal = v.getAttribute('href');
  v.setAttribute('href', 'sip:' + elemVal);
});

Upvotes: 2

kind user
kind user

Reputation: 41893

If I've understood your correctly, you just want to add sip: at the beginning of the href attr?

var elems = document.getElementsByClassName('skypemail');

Array.from(elems).forEach(function(v){
  var elemVal = v.getAttribute('href');
  v.setAttribute('href', 'sip:' + elemVal);
});
<a class="skypemail" href="[email protected]">test</a>
<a class="skypemail" href="[email protected]">test</a>
<a class="skypemail" href="[email protected]">test</a>
<a class="skypemail" href="[email protected]">test</a>

Upvotes: 3

Grynets
Grynets

Reputation: 2525

If you want to modify your element you can do something like this:
const sm = document.querySelector('#skypemail'); sm.href = '<here you can set whatever want for href link>'

Upvotes: 1

Related Questions