Currently, I am working on a feature to generate a slug dynamically using Javascript. I want my users to be able to preview the slug before submitting the form. Below is the Javascript code I have written:
function createSlug(text) {
return text
.toString()
.toLowerCase()
.normalize('NFD')
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-');
}
function updateSlug(text) {
document.getElementById("slug").value = createSlug(text);
}
Here is the HTML structure I am using, leveraging Bootstrap 4:
<form class="form">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" onkeyup="updateSlug(this)" id="name" name="name" placeholder="Example input placeholder">
</div>
<label for="slug">Custom URL</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon3">{{ env('APP_URL') }}/listing/</span>
</div>
<input type="text" class="form-control" id="slug" name="slug" aria-describedby="basic-addon3">
</div>
</form>
I encountered an error stating that the function 'updateSlug' does not exist. I am using Laravel mix to compile my code. Can someone guide me on what I might be doing wrong?