Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2005, 10:29 AM
Registered User
 
Join Date: Feb 2005
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!

Code:
<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;
}
</script>
And here are the select box and text fields in question.

Code:
<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 />
 
Old February 5th, 2005, 12:17 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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:
Code:
return_text.replace(/^\u00a0+/);
(The ^ means only spaces at the start of the text.)



--

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

Ah, that works perfectly now, thanks Joe :D






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





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