1

I'm creating a to-do list that allows the user to add and check off tasks. I was wondering if there was a way to style the actual checkbox for every task the user adds.

Edit: So I've edited my CSS code but I'm still not sure what I'm doing wrong.

$(() => {
  $('input').on('keypress', function(e) {
    if (e.keyCode == 13) {
      const newTask = $(this).val();
      if (newTask) {
        var li = $("<li><input type='checkbox' id='newtasklist' class='right-margin' <label>" + newTask + "</label></li>");
        $('#tasksUL').append(li);
        $(this).val("");
      }
    }
  });

  $('body').on('click', ':checkbox', function(e) {
    $(this).parent().toggleClass('selected');
  });
});
.selected { 
  text-decoration: line-through; 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<input type="text" name="newtask" value="" spellcheck="false" placeholder="New Task" id="newtask">
<ul id="tasksUL">
  <li><input type="checkbox" id="newtaskitem" class="right-margin"><label>Welcome to Droplet's 'Tasks' feature!</label></li>
</ul>

CSS code that I tried:

.selected { 
  text-decoration: line-through; 
  font-size: 20px;
}
.right-margin{
  margin-right: 30px;
}

input[id="newtasklist"] {
  background: url('checked.png');
  background-size: 100%;
}
Aurora123
  • 135
  • 3
  • 13
  • 1
    What is the question here? See also [background](https://developer.mozilla.org/en-US/docs/Web/CSS/background). – Teemu Jul 09 '19 at 04:23

1 Answers1

1

background-size specify the size of a background image, instead you can try font-size

$(() => {
  $('input').on('keypress', function(e) {
    if (e.keyCode == 13) {
      const newTask = $(this).val();
      if (newTask) {
        var li = $("<li><input type='checkbox' id='newtasklist' class='right-margin' <label>" + newTask + "</label></li>");
        $('#tasksUL').append(li);
        $(this).val("");
      }
    }
  });

  $('body').on('click', ':checkbox', function(e) {
    $(this).parent().toggleClass('selected');
  });
});
.selected { 
  text-decoration: line-through; 
  background: url('https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500');
  font-size: 20px;
}
.right-margin{
  margin-right: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<input type="text" name="newtask" value="" spellcheck="false" placeholder="New Task" id="newtask">
<ul id="tasksUL">
  <li><input type="checkbox" id="newtaskitem" class="right-margin"><label>Welcome to Test 'Tasks' feature!</label></li>
</ul>
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • Thanks, but I'm trying to style the actual checkbox. I've edited the code but I'm still not sure what I'm doing wrong. – Aurora123 Jul 10 '19 at 01:54
  • @Aurora123, please refer https://stackoverflow.com/questions/4148499/how-to-style-a-checkbox-using-css – Mamun Jul 10 '19 at 03:34