How to Convert Title to URL Slug using JavaScript ? Last Updated : 18 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Converting a title to a URL slug in JavaScript involves transforming the input text into a format suitable for URLs. This typically entails replacing spaces with dashes, removing special characters, and ensuring lowercase consistency for compatibility and readability in web addresses.Prerequisite HTML BasicsJavaScript BasicsProgram below will convert a title into a URL Slug using JavaScript.ApproachCreate an HTML form with input for the title and output for the URL slug with unique IDs.Add some CSS style to the element.Here, we have used the replace() function in JavaScript to make a string slug.Here, we use the trim() method to remove extra spaces from the input.The created slug string can be further used in URLs.Example: Below is the basic HTML code implementation: HTML <!DOCTYPE html> <html> <head> <style> fieldset.slugify { color: #515151; border: 1px solid #ccc; padding: 15px; } .slugify legend { font-size: 16px; font-weight: 600; padding: 0 10px; } .slugify input { display: block; padding: 8px; margin: 8px; } .slug-output { color: #05ab13; font-size: 20px; font-weight: 500; } </style> </head> <body> <form> <fieldset class="slugify"> <legend>GeeksforGeeks</legend> <label for="slug-source"> Input Title: </label> <input type="text" value="" id="slug-source" /> <label for="slug-target"> URL Slug: </label> <input type="text" value="" id="slug-target" /> <button type="button" onClick="myFunction()" > Convert </button> <p> <span class="slug-output"> Generated URL Slug </span >: <span id="slug-target-span" ></span> </p> </fieldset> </form> <script> function myFunction() { const a = document.getElementById("slug-source").value.trim().replace(/\s+/g, " ");; const b = a.toLowerCase().replace(/ /g, '-') .replace(/[^\w-]+/g, ''); document.getElementById("slug-target").value = b; document.getElementById("slug-target-span").innerHTML = b; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Convert Title to URL Slug using JavaScript ? S sankalpsharma424 Follow Improve Article Tags : JavaScript JavaScript-Questions Similar Reads How to Get the Current URL using JavaScript? Here are two different methods to get the current URL in JavaScript.1. Using Document.URL PropertyThe DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).Syntaxdocument.URLReturn Val 1 min read How to parse URL using JavaScript ? Given an URL and the task is to parse that URL and retrieve all the related data using JavaScript. Example: URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find hostname: geeksforgeeks.com path: /courses Method 1: In this method, we will use createElement() method 2 min read How to get URL Parameters using JavaScript ? To get URL parameters using JavaScript means extracting the query string values from a URL. URL parameters, found after the ? in a URL, pass data like search terms or user information. JavaScript can parse these parameters, allowing you to programmatically access or manipulate their values.For getti 3 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read How to Extract the Host Name from URL using JavaScript? Extracting the hostname from a URL using JavaScript means retrieving the domain part from a complete web address. This can be done using JavaScript's URL object or methods like window.location, which allow easy access to the hostname of a URL.What is URL?A URL (Uniform Resource Locator) is the web a 2 min read Like