Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 5th, 2005, 10:29 AM
Registered User
Join Date: Feb 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to delinear
Default Replacing nbsp

Hi all,

I'm trying to use replace to remove ' ' characters from a variable and having no luck whatsoever.

What I'm trying to do is take an indented drop-menu and apply an onChange event handler so that when someone selects an item from the menu the indentation is removed and the text that's left is placed in a separate text field. It does this so that the text in the text field can be edited and then updated in my database so that menu item names can be changed.

It's all working fine except the indentation isn't being removed. I know I can remove it server side in my script there, but it looks messy client side to have the leading spaces.

Here's my code, any help would be great!

<SCRIPT TYPE="text/javascript">
function selectItem(cat_id, cat_value){

    var option_name = document.getElementById(cat_id);
    var option_count = option_name.length;
    var option_value = document.getElementById(cat_id).value;

    for(i = 0; i < option_count; i++){
        if(option_name[i].value == option_value){
            return_text = option_name[i].text;
            document.getElementById(cat_value).value = return_text.replace(/&nbsp;/gi,'');
    return false;
And here are the select box and text fields in question.

<select name="parent_id" id="select_cat" onChange="selectItem('select_cat','cat_edit');">
    <option value="1">Programming</option>
    <option value="2">&nbsp;&nbsp;PHP</option>
    <option value="4">&nbsp;&nbsp;&nbsp;&nbsp;Tutorials</option>
    <option value="7">&nbsp;&nbsp;&nbsp;&nbsp;Scripts</option>
    <option value="3">&nbsp;&nbsp;ASP</option>
    <option value="8">&nbsp;&nbsp;JavaScript</option>
    <option value="5">Design</option>
    <option value="6">&nbsp;&nbsp;Graphic Design</option>
    <option value="9">&nbsp;&nbsp;Web Design</option>
</select>&nbsp;&nbsp;Select category to edit<br /><br />

<input type="text" name="title" id="cat_edit" />&nbsp;&nbsp;Edit category<br />
Reply With Quote
  #2 (permalink)  
Old February 5th, 2005, 12:17 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

You should be able to use a unicode expression. The code for non breaking space is (in hex) 00A0. If all the spaces are at the start it's better to use the + operator rather than the g switch:
(The ^ means only spaces at the start of the text.)


Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old February 5th, 2005, 12:22 PM
Registered User
Join Date: Feb 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to delinear

Ah, that works perfectly now, thanks Joe :D

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing value with symbol in Gridview? kingroon ASP.NET 2.0 Basics 2 July 7th, 2008 09:46 AM
Help replacing pannels MacDevv C# 0 August 14th, 2006 04:35 AM
Replacing characters in a string semilemon C# 2005 2 June 16th, 2006 11:31 PM
Replacing tabel civa Access VBA 1 January 24th, 2005 08:41 AM

All times are GMT -4. The time now is 05:25 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.