Auto Permalink value from one text field to another in Jquery. This code will convert Text to url friendly and auto put to another text field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" ></script> <form id="form1" name="form1" method="post"> <label for="name">Name:</label> <input type="text" name="name" id="name"> <label for="permalink">Permalink:</label> <input type="text" name="permalink" id="permalink"> <input type="submit" name="submit" id="submit" value="Submit"> </form> <script> 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 } $("#name").keyup(function(){ var sl_val=this.value; var sl_val= slugify(sl_val); $("#permalink").val(sl_val); }); </script> |