SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jquery Copy TextBox & Replace Spaces With Hyphens

    Trying to copy the contents of a first textbox as it is typed and replace spaces with hyphens - used this code but it only replaces the first space with hyphen subsequent spaces are nor replaced ... If anyone can help would appreciate, I'm not very good with JQuery .... thanks ....

    Code:
    HTML Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>TEST</title>
    <SCRIPT language="javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></SCRIPT>
    
    
    
    
    
    
    <SCRIPT type="text/javascript">//<=!=[=C=D=A=T=A=[ 
    $(window).load(function(){
    $("#title").on("keydown", function () {
    
    
       $("#url").val($(this).val().toLowerCase().replace(" ", "-"));
    });
    
    
    });//]=]=>  
    
    
    </SCRIPT>
    
    
    </head>
    
    
    <body>
    <input type="text" id="title" />
    <input type="text" id="url" />
    
    
    </body>
    </html>
    Last edited by Force Flow; Feb 15, 2014 at 22:51. Reason: added code tags

  2. #2
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to replace globally. At the moment you are only changing the first space found. To do this write
    .replace(/ /g,"-") instead of your .replace(" ","-").

    The two forward slashes surround a space character, so you are saying the same thing. As an example, if you put another character instead of the space between the two slashes it will be replaced with the hyphen along your test string.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •