Click here to Skip to main content
11,709,220 members (71,206 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Javascript
Hello All,

I have one text box control in my page. I have to show this text box value in another label(through javascript) on clicking preview button. My problem is if I given like
'Naveen_____________kumar'(Here _____ means white space) it is coming like 'Naveen kumar'. But I have to show it like as it is given in text box (with spaces).

can any one help me on this.

My javascript:

function funGetPreview() {
       var txtSubject = document.getElementById(uniqueId + "txtSubjectLine");
        var lblSubject = document.getElementById("lblPreviewSubValue");
        lblSubject.innerHTML = txtSubject.value;
}
Posted 4-Apr-13 0:51am
Comments
NaVeN Kumar at 4-Apr-13 7:42am
   
Thanks every one for the help....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Instead of innerHTML use innerText property

function funGetPreview() {
       var txtSubject = document.getElementById(uniqueId + "txtSubjectLine");
        var lblSubject = document.getElementById("lblPreviewSubValue");
        lblSubject.innerText = txtSubject.value;
}
  Permalink  
Comments
NaVeN Kumar at 4-Apr-13 7:39am
   
Thanks Rajesh... It worked...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Try this:
lblSubject.innerHTML = txtSubject.value.split(' ').join(' ');
Hope this helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

ProgramFOXs solution will work, but a much, much more efficient solution would be to use string.Replace:
lblSubject.innerHTML = txtSubject.value.replace(" ", " ");
  Permalink  
Comments
enhzflep at 4-Apr-13 7:23am
   
That's what I thought. Isn't it php that will replace all instances of needle, while javascript will only do the first? (without a regex, that is)
OriginalGriff at 4-Apr-13 7:27am
   
My mistake - you need to specify global:
...replace(/ /g, " ");
enhzflep at 4-Apr-13 7:33am
   
Ah-hah. Thought that to be the case, but secretly thought I may just learn another trick.
Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Well, it seems likely that your problem is not one of "how do I insert spaces into a string" but rather one of "how can I display several space characters in a row in a web-page"

You may well wish to escape the text entered by the user.
So, if the user enters
Naveen     Kumar
, you will have a string that contains the text
"Naveen     Kumar"
However, when you display it, the browser will turn those 5 spaces "_____" into a single one.

If you use the javascript string replace function, it will only replace the first occurance of a string.
Code:
var str = "Naveen_____Kumar";
str.replace("_", ".");
Result:
str = "Naveen.____Kumar";


Code:
var str = "Naveen_____Kumar";
var regex = new RegExp('_', 'g');
str = str.replace(regex, ".");            // change "." to " "
Result:
str = "Naveen.....Kumar";


Lastly, the code for an escaped space (' ') character is ' ' - you can replace the text "." with " "
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 OriginalGriff 244
2 Maciej Los 225
3 Peter Leow 219
4 Mika Wendelius 160
0 OriginalGriff 9,348
1 Sergey Alexandrovich Kryukov 8,727
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100