Search code examples
jqueryreplacelowercase

Replace & lowercase in Jquery


I have textbox which on typing the text need to get copy into other textbox and also the spacing between the words need to replace with dash and it should get converted into lower Case.

I have below code but its not working

$(document).ready(function() {
$('#Name').keyup(function(e) {
    var txtVal = $(this).val();
    txtVal = txtVal.toLowerCase();
    $('#URL').val(txtVal);
});

});

Need HELP, what can be done??


Solution

  • After replacing to lower case, replace all [space] with -

    $(document).ready(function() {
      $('#Name').keyup(function(e) {
        var txtVal = $(this).val();
        txtVal = txtVal.toLowerCase().replace(/\s/g, '-');
        $('#URL').val(txtVal);
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input id="Name" />
    <input id="URL" />