Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript eats right spaces

Status
Not open for further replies.

edfimasa

Programmer
Apr 9, 2008
21
0
0
PT
Hi.

I have two problems with javascript.

One it's that if i have var x = "aaaa ";
it will show "aaaa"
How do i prevent this from happening in jS?

The other problem is with a textarea
var textarea = document.getElementById("myTextArea");
var begin = "a begining<b>";
var end = "</b>some other stuff";
textarea.value = begin.substr(0,begin.length-4) + end.substr(4,end.length);

the problem is that in the textarea, when the text shows, it doesnt start from the begining of the textbox, but a space ahead.

Can you help with both problem?


Cheers.
 
One it's that if i have var x = "aaaa ";
it will show "aaaa"
How do i prevent this from happening in jS?

Er... no, JS isn't eating the space. So, tell us how you know the space isn't there if it has no visible characteristics? For example, if you alert 'x', you'd see no space, would you?

The only problem I can remember having in the last 8 years of JS coding to do with whitespace was IE only, and it ate both leading and trailing whitespace on certain XML docs.

Perhaps posting a URL and explaining how you reproduce your issue would help, because I can pretty much guarantee the problem is in your code, not an error with JS itself.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Well, i use IE 6

And if you understood i was saying the problem was with JS,then I'm sorry maybe i expressed myself poorly.

In the substring i was going to the wrong position, and therefore not retrieving the space.
That is solved i guess.

What about the textarea problem?


Cheers.
 
Again, there's no black magic / dark arts needed to set the value of a textbox. No extra spaces will be inserted, and nothing will be deleted... so, I would say that as with the first issue you have, the error is with your code.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
re: Textarea

Works exactly as expected on IE 6 and Firefox 2:
"a begininsome other stuff
 

this is to remove <br>some text</br> from the textarea for example
I don't see any problem with my code, althow i don't doubt you are right when saying its on my code.
so ie. :
...
var begin ="<br>";
var end = "</br> other stuff";
var selection = "some text";

textarea.value = begin.substr(0,begin.length-3) +selection + end.substr(4,end.length);
//code so solve the problem of a space in the begining
if(begin.charAt(0) != " ")
if(textarea.value.charAt(0) == " ")
textarea.value = textarea.value.substr(1,textarea.value.length);
...

Can you tell me what is wrong so i can remove the final lines after the comment?

Cheers.
 
I don't understand why you would want to do any of that substring suff to remove "<br>" tags from a textarea value property.

Why not use a search + replace instead?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Well i can't use replace because i can have alot of same html tags in the text.

The point is to format text in a textarea with html tags.
(kinda of a text editor)

I can add a tag or remove the tag, and the problem is when i remove the tag.

I think the problem is here
begin.substr(0,begin.length-3) if lenght is 3, it will be the same as begin.substr(0,0) and that for some reason is returning a blank, i didn't tested my theory yet.


What do you think?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top