<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>Javascript Auto Slugify</h2>
<form class="form">
<div class="form-group">
<input type="text" class="form-control title" placeholder="Type anything...">
</div>
<div class="form-group">
<input type="text" class="form-control slug">
</div>
</form>
</div>
</div>
function slugify(text) {
// Source: https://gist.github.com/mathewbyrne/1280286
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
.replace(/[\s_-]+/g, '-'); // swap any length of whitespace, underscore, hyphen characters with a single -
}
$('.title').keyup(function(){
console.log($(this).val());
$slug = slugify($(this).val());
$('.slug').val($slug);
});