-1

i tried doing a loop but that doesn't exactly work

i thought of trying regex as well but that doesn't work either

this code is a part of a bigger that uses jquery to dynamically add fields when user presses add new and deletes those when user presses delete so the classes and ids used have a pattern they follow. ie. this will be solved if i can do $(like .'/deleteimg[0-9]{1,}/') instead of the exact thing.

the solution i have in mind right now is writing a code that writes this code but while that relieves me of writing the code it doesn't exactly help the code so i don't want to do that unless its the only option

ill be surprised if I'm the first person to ever run into this problem but googling didn't really reveal any relevant answers

jQuery(function($){
$('.deleteimg1').click(function() {
    $('.1').remove();
});
});
jQuery(function($){
$('.deleteimg2').click(function() {
    $('.2').remove();
});
});
jQuery(function($){
$('.deleteimg3').click(function() {
    $('.3').remove();
});
});
jQuery(function($){
$('.deleteimg4').click(function() {
    $('.4').remove();
});
});
jQuery(function($){
$('.deleteimg5').click(function() {
    $('.5').remove();
});
});
jQuery(function($){
$('.deleteimg6').click(function() {
    $('.6').remove();
});
});
jQuery(function($){
$('.deleteimg7').click(function() {
    $('.7').remove();
});
});
jQuery(function($){
$('.deleteimg8').click(function() {
    $('.8').remove();
});
});
jQuery(function($){
$('.deleteimg9').click(function() {
    $('.9').remove();
});
});
jQuery(function($){
$('.deleteimg10').click(function() {
    $('.10').remove();
});
});
Vaibhav
  • 3
  • 3
  • 1
    A snippet is only useful when there's something to execute and that helps to understand the actual problem. And if you use a snippet then also use the _"Tidy"_ function. – Andreas Aug 03 '21 at 10:16
  • Please reduce the number of functions (we also get the "idea" with only 3 or 4 of them) and also add the relevant markup -> [mcve] – Andreas Aug 03 '21 at 10:17
  • Please show the HTML, it really matters to providing a best practices answer. – T.J. Crowder Aug 03 '21 at 10:18

3 Answers3

1

You can use for-loop like:

jQuery(function($) {
  for (let i = 1; i <= 10; i++) {
    $('.deleteimg'+ i).click(function() {
      $('.'+ i).remove();
    });
  }
});
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
  • 2
    They *can*, yes, but it's not likely to be the best way to solve the problem. – T.J. Crowder Aug 03 '21 at 10:18
  • Why not? which's the problem? – Simone Rossaini Aug 03 '21 at 10:20
  • Aside from the use of an invalid class selector, it's fragile and uses a lot more handlers than are likely necessary. Again, though, it does *work* (apparently jQuery works around the invalid class seletor). :-) – T.J. Crowder Aug 03 '21 at 10:21
  • Can you provide the best solution :) ? I'm curious, another solution can be used data-attribute – Simone Rossaini Aug 03 '21 at 10:22
  • I can provide some *alternatives* that *I* think are better (but "better" is subjective), which I've done. :-) But it really depends on the missing information in the question: What the HTML structure is and whether they can change it. – T.J. Crowder Aug 03 '21 at 10:27
  • i tried something very similar to this but that didnt work .. surprisingly this one does tho .. thanks a lot !! – Vaibhav Aug 03 '21 at 10:30
  • 1
    @Nitheesh you use the same method as mine, simple instead of use `for-loop` you use `forEach` :) – Simone Rossaini Aug 03 '21 at 10:33
  • @Vaibhav - You may have used `var` rather than `let`, [details here](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work). The `let` and the fact it's declared in the `for` loop are important to Simone's answer. – T.J. Crowder Aug 03 '21 at 10:33
  • @Nitheesh - The person posting the question receives a notification when you post an answer, there's no need to write a comment on another answer advertising your answer. (Opinions can vary, but it seems inappropriate.) – T.J. Crowder Aug 03 '21 at 10:34
  • I think he ask to you ^^ @T.J.Crowder (or not ?) – Simone Rossaini Aug 03 '21 at 10:34
  • 1
    @SimoneRossaini I don't see an @ in Nitheesh's comment so I took it as a reply to the OP's comment just above. More proof that written communication is hard. :-D – T.J. Crowder Aug 03 '21 at 10:36
1

Whenever you see this kind of thing, the usual approach is to ask yourself "What common things can I factor out?" In this case, it's everything except the digit. Another way to phrase that (the converse) is "What are the things that change that I can parameterize (pass into a function)?" In this case, the answer is "the digit."

Without changing your HTML, we're left with an unwieldy selector or a loop as in Simone Rossaini's answer, but if you can change your HTML so that instead of:

<img class="deleteimg1">

you have

<img class="deleteimg" data-index="1">

Then it can be:

jQuery(function($) {
    $(".deleteimg").on("click", function() {
        const num = $(this).attr("data-index");
        $(`.${num}`).remove();
    });
});

(Except note that it's not valid to start a class name in a class selector with a digit. If it works with jQuery, that's something jQuery's fixing for you.)

Even better, if there's some structural relationship between the elements, like this:

<div>
    <img src="...">
    <input type="button" class="deleteimg" value="X">
</div>

Then it can just be (assuming you want to remove the entire structure, not just the img):

jQuery(function($) {
    $(".deleteimg").on("click", function() {
        $(this).closest("div").remove();
    });
});

But if you can't change your HTML, Simone Rossaini's answer factors out the digits via a loop.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Array.from(Array(10).keys()).forEach((index) => {
  $(`.deleteimg${index + 1}`).click(function () {
    $(`.${index + 1}`).remove();
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div>
  <button class="deleteimg1">deleteimg1</button>
  <div class="1">Node 1</div>
</div>
<div>
  <button class="deleteimg2">deleteimg2</button>
  <div class="2">Node 2</div>
</div>
<div>
  <button class="deleteimg3">deleteimg3</button>
  <div class="3">Node 3</div>
</div>
<div>
  <button class="deleteimg4">deleteimg4</button>
  <div class="4">Node 4</div>
</div>
<div>
  <button class="deleteimg5">deleteimg5</button>
  <div class="5">Node 5</div>
</div>
<div>
  <button class="deleteimg6">deleteimg6</button>
  <div class="6">Node 6</div>
</div>
<div>
  <button class="deleteimg7">deleteimg7</button>
  <div class="7">Node 7</div>
</div>
<div>
  <button class="deleteimg8">deleteimg8</button>
  <div class="8">Node 8</div>
</div>
<div>
  <button class="deleteimg9">deleteimg9</button>
  <div class="9">Node 9</div>
</div>
<div>
  <button class="deleteimg10">deleteimg10</button>
  <div class="10">Node 10</div>
</div>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49