How to identify equal classes in numerical order with jQuery?

Asked

Viewed 61 times

-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?

1 answer

1


You can pick up the content through the index():

$(document).ready(function(){
  $(".mudar").click(function(){
    var indice = $(this).index();
    $("#download").attr("href", "https://file"+indice+".zip");
  });
});

In your specific case, you could also take the number that it contains in the string and change the link through it:

var indice = $(this).html().replace( /^\D+/g, '');

Editing

After comments and chat conversations I created this solution:

First put the name of the files in the tag name of the buttons and a p with the custom file name that will be downloaded:

<button name="file1" class="mudar">zip 1 <p style="display: none">azul</p></button>
<button name="file2" class="mudar">zip 2 <p style="display: none">preto</p></button>
<button name="file3" class="mudar">zip 3 <p style="display: none">amarelo</p></button>

Then use this code:

$(document).ready(function(){
  $(".mudar").click(function(){
    var file = $(this).attr("name");
    var nameFile = $(this).find('p').html();
    var download = $("#download");
    download.attr("href", "https://"+file+".zip");
    download.attr("download", nameFile);
  });
});
  • 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?

  • @Thiagosoubra I will create a solution...

  • @Thiagosoubra takes a look at the edition

  • 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...

  • 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!

  • 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>

  • @Thiagosoubra I’m not sure what you want...

  • Do you want 3 different values for each download? Why this?

  • I can call you on chat?

  • @Thiagosoubra yes

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.