javascript - Replace string as you type -
i want create web-friendly image name user input on form. want replace spaces in user entered string dash user types.
my code replaces first space.
how replace all spaces dashes?
$('#form_model').keyup(function(event) { var newtext = event.target.value; newtext = newtext.replace(" ", "-"); $('#form_image').val(newtext+".png"); });
this done using regexpression g
flag. g
stand global, affects whole string , not first value.
here working fiddle:
$('#form_model').keyup(function(event) { var newtext = event.target.value; newtext = newtext.replace(/\s/g, "-"); $('#form_image').val(newtext+".png"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="form_model" type="text"> <input id="form_image" type="text" readonly>
Comments
Post a Comment