I have a question. I've been coding a jQuery event where the button
with an ID of add is ticked and is updated to an input
Here is the jQuery code
/* Change the Add button to an Input element*/
$("#add").click(function(){
$(this).replaceWith("<input type='text' placeholder='Task'>");
$("div.tasks>span").remove();
});
/* Adding a value to the list */
$("input").keydown(function(e){
if(e.keyCode == 13){
var item = $(this).val();
$(this).parent().prepend("<li>" + item + "</li>");
$(this).val("");
}
});
Here is the HTML:
<header class="content-header">
<img src="img/icon.png" alt="icon" class="header-icon">
<h1>Monday</h1>
<img src="img/icon.png" alt="icon" class="header-icon flipped">
<!-- <p>6 <span>Tasks</span></p>-->
</header>
<div class="tasks">
<hr>
<button id="add"></button>
<span> Add task</span>
<input type="text">
</div>
Whats supposed to happen here is once the Button has been ticked it will now turn to an input
element.
And once the input
element has been populated with data and keycode==13
is pressed (Enter). The value placed on the input
element will now be added to the DOM within a li
element.