I have a code that works to replace the url of a link with a given id
I want this code to work for multiple links and player with the same specified id or class
I have one button that should change all together
Can you guide me or help me?
$(document).ready(function(){
$('#change').on('click', function(e) {
var purl = $("#urlchange").attr("href");
purl = purl .replace("dl.faz2music.ir", "up.faz2music.ir");
$("#urlchange").attr("href",purl);
notice.showToast({text: 'Links converted',type: 'success',showClose: true});
});
});
<button id="change" class="ltr">change link's</button>
<a href="https://dl.faz2music.ir/download/3712434/Ahmad Solo - Shazdeh Pesar [320].mp3" id="urlchange">Download</a>
<a href="https://dl.faz2music.ir/download/3712434/Ahmad Solo - Shazdeh Pesar [128].mp3" id="urlchange">Download</a>
<audio><source type="audio/mp3" src="https://dl.faz2music.ir/download/3712434/Ahmad Solo - Shazdeh Pesar [320].mp3" id="urlchange"></audio>
<audio><source type="audio/mp3" src="https://dl.faz2music.ir/download/3712434/Ahmad Solo - Shazdeh Pesar [128].mp3" id="urlchange"></audio>
This code replaces only one link
I want to be able to do the same for several links with the same ID or Class
Jquery
3
Answers
Using each and a class:
You need to assign same class to anchor tags as you can not assign same ids to multiple tags
As you’re using jquery, note that
.attr
has an overload which takes a callback:http://api.jquery.com/attr/#attr-attributeName-function
So, combining with using a class as suggested in other comments/answers, your code becomes:
Updated snippet: