View Full Version : onkeyup copy text from one field to another

06-20-2009, 10:06 AM

I have two text fields, 1) Page Title and 2) URI. As and when the user types in the Page Title, I want the text to be copied straight into the URI text field, but after having made all letters small and replacing ' ' (space) with '-' minus sign.

This is what I have so far:

<script type = "text/javascript">
function transfer(which) {
reg = /\s+/;

document.getElementById("uri").value = which.toLowerCase().replace(reg,'-');
<input type="text" name="title" id="title" value="" size="32" onkeyup = "transfer(this.value)">
<input type="text" name="uri" id="uri" value="" size="32">

case lowering works fine, but with space replacement, it only replaced the first space with '-', and leaves the rest of the spaces as they are.

Hence, this is how it looks:

Page Title: This is my Pompous Page Title
URI: this-is my pompous page title

How can I get it to continue replacing all spaces and not just the first one?


Philip M
06-20-2009, 10:34 AM
function transfer(which) {
reg = /\s+/g; // the g switch means global.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.