2

What is a good way to write this script more dynamic:

I have a variable which can have a value up to 18.

If $count is 1 I want to remove everything up to 18.

If $count is 2 I want to remove everything up to 18.

And so on.

        if($count == 1) {
            $('#input-1').remove();
            // remove up to 18
        }
        if($count == 2) {
            $('#input-2').remove();
            // remove up to 18
        }
        if($count == 3) {
            $('#input-3').remove();
            // remove up to 18
        }
        ...
        if($count == 18) {
            $('#input-18').remove();
        }

I did this:

            for(var i = 0; i < 18; i++) {
                if($count== i) {
                    $('#input-'+i).remove();
                }
            }

but it does not remove the divs from i to 18

EDIT: (to be clear)

I want to check each, like $count == i then I want to remove from i to 18

3
  • 1
    Use a loop: for (var i = $count; i <= 18; i++) { ... } Commented Mar 22, 2017 at 12:22
  • 1
    Your question is a bit odd. Can you please add the HTML too? Commented Mar 22, 2017 at 12:22
  • Could use more information regarding this question. Commented Mar 22, 2017 at 12:22

6 Answers 6

2

This should do:

for(var i = $count; i <= 18;i++){
   $('#input-'+i).remove();
}

EDIT

I want to check each, like $count == i then I want to remove from i to 18

The outer for loop allows us to check if i is equal to $count, if the expression is satisfied (true) then we simply begin another loop starting at i. Within the second for loop, we begin removing the elements from i and beyond.

for(var i = 1; i <=18; i++) {
    if($count == i) {
       for(var j = i; j <= 18; j++){
         $('#input-'+j).remove();
       }
      return;
    }
}

Note - This will send a return value of undefined to the caller. However, you can specify a different return value if you wish to.

Sign up to request clarification or add additional context in comments.

8 Comments

but I want to check each like $count == i then remove from i to 18
@J.Alan okay i will do another solution for that if you like.
@J.Alan just do me a favour and update your question with the comment you've just written --> " but I want to check each like $count == i then remove from i to 18"
Seems to work so far I will test it again after a few hours then I am going to tick your answer :D
Thanks alot everything is clear to me now, have a nice week!
|
1

Something like this $("#input-"+$count).remove()

2 Comments

Based upon OP's stated intention - this makes the most sense.
Okay - now the question was edited - this no longer works
0

You can use a simple for loop

for(let i=$count;i<=18;i++){
    $('#input-'+i).remove();
}

This will iterate starting from $count to 18 and will remove all input with id between $count and 18

1 Comment

but I want to check each like $count == i then remove from i to 18
0

The easiest method would be a loop with a variable using Template-Strings

const addElements = function() {
  // Add elements dynamically
  for (let i = 0; i < 10; i++) {
    const div = `<div id="element-${i}">I\'m ${i}</div>`;
    $('body').append(div);
  }
}

const removeElements = function() {
  // Remove elements dynamically
  for (let i = 0; i < 10; i++) {
    const $div = $(`div#element-${i}`);

    $div.remove();
  }
}


$('#addBtn').on('click', function() {
  addElements();
});

$('#removeBtn').on('click', function() {
  removeElements();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="removeBtn">Remove</button>
<button id="addBtn">Add</button>

Comments

0

Use the following.!

for (var i = $count; i <= 18; i++) {
  $('#input-' + i).remove();
}

2 Comments

but I want to check each like $count == i then remove from i to 18
@Andreas noted. Thanks.. (y)
0

I believe you want this:

var start=$('input-'+$count);
var end=$('#input-18').index();
$('input').slice(start,end).remove();

If the only inputs you have are $('input-1) to $('#input-18'), you can skip the check and do:

  $('input').slice($count-1).remove();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.