I am trying to set the value of slug field by manipulating the input of another field title using JavaScript. When I type in the slug and then again i start typing in the title field the value of slug field doesn't get updated. here is my form
document.getElementById('title').addEventListener("input", function(){
let title = document.getElementById('title').value;
console.log(title);
title = title.toLowerCase();
title = title.replace(/\s+/g, '-');
document.getElementById('slug').setAttribute("value", title);
console.log(document.getElementById('slug').value);
});
<form method="POST" action="http://laravel.dv/admin/blog/post/store" id="create-post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="2HeXBBKd1HSmpuGvjYqF1KygbGsCQaZtkuUthi1s"> <div class="page-title">
<h1>Create A Blog</h1>
</div>
<br>
<div class="row">
<div class="col-12">
<div class="form-control">
<input type="text" id="title" name="title" value="" placeholder="Title" >
</div>
<div class="form-control">
<input type="text" id="slug" name="slug" value="" placeholder="Slug" >
</div>
<div class="form-control">
<input type="file" name="image" placeholder="Image upload">
</div>
<div style="margin: 0 0 20px;">
<textarea name="body" id="summernote"></textarea>
</div>
</div>
<div class="col-12">
<input type="submit" name="btnSubmit" class="btn-primary" value="Save Post" />
</div>
</div>
</form>