Click here to Skip to main content
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 1: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 Kornfeld Eliyahu Peter 275
1 Maciej Los 204
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 183
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
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