Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change some text to lowercase and replace spaces with hyphens using jQuery?

I have a hidden input field that will take the value of another on keyup and I'm trying to figure out how transform the value in the hidden field to lowercase and replace spaces with hyphens.

So, if someone types "This Is A Sample" in the title input field, the identifier input field will be set to "this-is-a-sample".

<input type="text" name="title" value="This Is A Sample" /> <input type="hidden" name="identifier" value="this-is-a-sample /> 
like image 228
KarmaKarmaKarma Avatar asked Sep 29 '10 16:09

KarmaKarmaKarma


2 Answers

This will replace all spaces with -

<script type="text/javascript"> $(document).ready(function(){ var test= $('input[name="title"]').val(); test = test.toLowerCase().replace(/ /g, '-'); $('input[name="identifier"]').val(test); }): </script> 
like image 79
Alpesh Avatar answered Sep 28 '22 11:09

Alpesh


To convert to lowercase :

var lowercase = 'This Is A Sample'.toLowerCase(); 

To Replace empty space:

var Replace= 'This Is A Sample'.replace(/ /g,"-"); 

take a look at this example

@JSbin

like image 23
Nick Avatar answered Sep 28 '22 10:09

Nick