make slug in javascript or jquery

** make slug in javascript or jquery::

<script type="text/javascript">

//Make Slug::

function slugify(text)

{

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

}




$('#page_slug').on('keyup change', function(){ $('#page_slug').val(slugify(($(this).val()))); });

 

OR

 

$('#page_slug').on('blur', function(){ $('#page_slug').val(slugify(($(this).val()))); });

 

</script>
Please follow and like us:
0