Javascript to remove spaces from a textbox value

You can use document.getElementsByName to get hold of the element without needing to go through the form, so long as no other element in the page has the same name. To replace all the spaces, just use a regular expression with the global flag set in the element value's replace() method:

var el = document.getElementsByName("10010input")[0];
var val = el.value.replace(/\s/g, "");
alert(val);

You need to "generalize" that regexp you're using so it's applied to all matches instead of just the first. Like this:

val = val.replace(/\s/g, '')

Notice the 'g' that modifies the regexp so it becomes "general".


Here is a function I use to replace spaces.

function removeSpaces(val) {
   return val.split(' ').join('');
}