On my page I have dozens of buttons that, when clicked, change the link ( href
) of a a
element, with the attr
method. To avoid putting a class on each button, since there are many, I would like jQuery to identify the buttons with class .mudar
in numerical order.
How I tried:
$(document).ready(function(){
$(".mudar")[0].click(function(){
$("#download").attr("href", "https://file1.zip");
});
$(".mudar")[1].click(function(){
$("#download").attr("href", "https://file2.zip");
});
$(".mudar")[2].click(function(){
$("#download").attr("href", "https://file3.zip");
});
});
<a href="" id="download">baixar</a>
<button class="mudar">zip 1</button>
<button class="mudar">zip 2</button>
<button class="mudar">zip 3</button>
It did not work because I do not understand how this works. Can you help me with this?