How to get data from a html input field and reformat it?

I basically have no knowledge of Javascript, and since I code very infrequently, what little I did know is long forgotten.

I want the ability to convert the data entered in a form input field, and change it and display it in another form input field.

In the conversion, I need to:

  • replace all spaces with a dash
  • strip out all brackets, periods, underscores, and single quotes
  • uppercase to lowercase


  1. Is there a better way to do the replacement of characters and spaces other than how I am doing it?
  2. Why does my current code not appear to work? The console outputs no value as it seems to be reading “sampleText” as being blank?

Well lets start with the second question first. Think about when this script is being executed. It is being run as soon as the HTML is rendered right? At that time, what is in the textbox? nothing. You will need to make sure that this code is run later after you have had a chance to put some text in it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.