Skip to content Skip to sidebar Skip to footer

How To Replace Whitespace With Underscore When Copy Value From Form Field To Another

Someone should be able to find this example for me or give an example.. After hours of searching I found the answer using some bizare search terms in Google only for my 15 month ol

Solution 1:

A little jQuery plugin to do something like this (this would put the value of the first matched element into the set of elements matched by the passed in selector):

$.fn.copyTo = function(selector) {
    $(selector).val($(this[0]).val().replace(/\s/g, "_"));
};

Example usage:

$("#source").copyTo("#dest");

Here's a working example.

Solution 2:

To copy the value from source, to dest, while replacing whitespace with an underscore, this should do it.

$("#dest").val($("#source").val().replace(' ', '_'));

Or to get any whitespace

$("#dest").val($("#source").val().replace(/\s/g, '_'));

Post a Comment for "How To Replace Whitespace With Underscore When Copy Value From Form Field To Another"