I have the following piece of code in a script tag that I need to get working for all #tel ID elements on the page. I can only get it to work for the first #tel element. I’ve been trying to use .each function – but no luck…
The reason being is that I can’t seem to get the ACF repeater URL to suit my needs here. The Advanced section of the dynamic content link part is not displaying. So I am trying to make a hack in an HTML widget for this. But I need it to work for all buttons with button ID #tel.
Here’s the code:
JavaScript
x
4
1
var link = document.getElementById('tel');
2
var href = link.getAttribute('href');
3
4
link.setAttribute('href', href.replace('http://', 'tel:'));
JavaScript
1
9
1
<div class="elementor-button-wrapper">
2
<a href="http://44400907" class="elementor-button-link elementor-button elementor-size-xs" role="button" id="tel">
3
<span class="elementor-button-content-wrapper">
4
<span class="elementor-button-text elementor-inline-editing" data-elementor-setting-key="text" data-elementor-inline-editing-toolbar="none">
5
44 40 09 07
6
</span>
7
</span>
8
</a>
9
</div>
Advertisement
Answer
In HTML/javascript element IDs must be unique. So in your case you can use class for that:
JavaScript
1
8
1
var links = document.querySelectorAll('.tel');
2
for(let i = 0; i < links.length; i++)
3
{
4
let link = links[i];
5
var href = link.getAttribute('href');
6
7
link.setAttribute('href', href.replace('http://', 'tel:'));
8
}
JavaScript
1
23
23
1
<div class="elementor-button-wrapper">
2
<a href="http://44400907" class="elementor-button-link elementor-button elementor-size-xs tel" role="button">
3
<span class="elementor-button-content-wrapper">
4
<span class="elementor-button-text elementor-inline-editing" data-elementor-setting-key="text" data-elementor-inline-editing-toolbar="none">44 40 09 07</span>
5
</span>
6
</a>
7
</div>
8
9
<div class="elementor-button-wrapper">
10
<a href="http://44400908" class="elementor-button-link elementor-button elementor-size-xs tel" role="button">
11
<span class="elementor-button-content-wrapper">
12
<span class="elementor-button-text elementor-inline-editing" data-elementor-setting-key="text" data-elementor-inline-editing-toolbar="none">44 40 09 08</span>
13
</span>
14
</a>
15
</div>
16
17
<div class="elementor-button-wrapper">
18
<a href="http://44400909" class="elementor-button-link elementor-button elementor-size-xs tel" role="button">
19
<span class="elementor-button-content-wrapper">
20
<span class="elementor-button-text elementor-inline-editing" data-elementor-setting-key="text" data-elementor-inline-editing-toolbar="none">44 40 09 09</span>
21
</span>
22
</a>
23
</div>