Reputation: 145
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%;
}
Upvotes: 1
Views: 108
Reputation: 68933
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>
Upvotes: 1