-1
On my page I have dozens of buttons that, when clicked, change the link (href
) of a single element a
, with the method attr
. To avoid putting a class on each button as there are many, I would like jQuery to identify the buttons with the 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 didn’t work because I don’t understand how it works. Can you help me with that?
Buddy, it would look great like this, the problem is that only in my example that the files (1.zip, 2.zip, 3.zip) follow this pattern, in real are files with different names, only the classes that really are equal... But I have the option of the files also in numerical order, the problem is that I would need them to be renamed when they were called to download, like the download="file name" that can be put in the <a> tag. So basically, either I need it to be possible to customize the links of each one or the name of the files when they are downloaded. Is there any solution to this?
– Thiago Soubra
@Thiagosoubra I will create a solution...
– Andrei Coelho
@Thiagosoubra takes a look at the edition
– Andrei Coelho
Andrei, thanks, but there is still a problem kkkk Here’s the thing, let’s say that I have files: 1.zip, 2.zip, 3.zip etc... I need that when they are downloaded their names are changed to, example, black.zip, blue.zip, yellow.zip. I could make it happen, for example, like this: <a href="1.zip" download="black">. Because then, when it was downloaded, it would appear black.zip to the user. Can you understand? Another way I could use it would be the files already with the names on them, but then I wouldn’t be able to use this solution of yours to catch them by a pattern...
– Thiago Soubra
For example, if there in "name" took what had written there and put before .zip... a logic like $("#download"). attr("href", "https://"+name+".zip"). It would be super right!
– Thiago Soubra
My htm would be:<button name="black" class="change">zip 1</button> <button name="blue" class="change">zip 2</button> <button name="yellow" class="change">zip 3</button>
– Thiago Soubra
@Thiagosoubra I’m not sure what you want...
– Andrei Coelho
Do you want 3 different values for each download? Why this?
– Andrei Coelho
I can call you on chat?
– Thiago Soubra
@Thiagosoubra yes
– Andrei Coelho
Let’s go continue this discussion in chat.
– Andrei Coelho