Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
function funGetPreview() {
       var txtSubject = document.getElementById(uniqueId + "txtSubjectLine");
        var lblSubject = document.getElementById("lblPreviewSubValue");
        lblSubject.innerHTML = txtSubject.value;
}
Posted
Comments
NaVeN Kumar 4-Apr-13 7:42am    
Thanks every one for the help....

Instead of innerHTML use innerText property

JavaScript
function funGetPreview() {
       var txtSubject = document.getElementById(uniqueId + "txtSubjectLine");
        var lblSubject = document.getElementById("lblPreviewSubValue");
        lblSubject.innerText = txtSubject.value;
}
 
Share this answer
 
Comments
NaVeN Kumar 4-Apr-13 7:39am    
Thanks Rajesh... It worked...
Hi,

Try this:
JavaScript
lblSubject.innerHTML = txtSubject.value.split(' ').join(' ');

Hope this helps.
 
Share this answer
 
ProgramFOXs solution will work, but a much, much more efficient solution would be to use string.Replace:
JavaScript
lblSubject.innerHTML = txtSubject.value.replace(" ", " ");
 
Share this answer
 
Comments
enhzflep 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 4-Apr-13 7:27am    
My mistake - you need to specify global:
...replace(/ /g, " ");
enhzflep 4-Apr-13 7:33am    
Ah-hah. Thought that to be the case, but secretly thought I may just learn another trick.
Thanks.
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:
JavaScript
var str = "Naveen_____Kumar";
str.replace("_", ".");

Result:
JavaScript
str = "Naveen.____Kumar";



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

Result:
JavaScript
str = "Naveen.....Kumar";



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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900