Google Answers Logo
View Question
 
Q: White space in javascript created form elements under NS ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: White space in javascript created form elements under NS
Category: Computers > Programming
Asked by: padders-ga
List Price: $2.00
Posted: 01 Nov 2002 05:50 PST
Expires: 01 Dec 2002 05:50 PST
Question ID: 95276
Hi there,

I am creating some select menu form elements using javascript. There
are in a category depth format like

> category
> > sub category
> > > sub category 1.

however I don't want to use > i want to just use white space. This
works fine under IE, (using just a space) but does not work in NS
(spaces seem to be ignored). I have tried   but that is just
displayed as the characters and not the HTML entity.

Any ideas?

Request for Question Clarification by hammer-ga on 01 Nov 2002 05:57 PST
As a help to the researchers, please post your JavaScript, if
possible. JavaScript  can be very picky about syntax and the
differences between what different browsers want are numerous and
subtle.

Request for Question Clarification by iaint-ga on 01 Nov 2002 06:01 PST
Hello padders

This is somewhat peculiar -- earlier this week I wrote some javascript
code to something almost identical and it renders perfectly in both IE
and Netscape, versions 4.7, 6 and 7.

Could you post a snippet of your code so that we can possibly see
where the problem might lie?

Regards
iaint-ga

Clarification of Question by padders-ga on 01 Nov 2002 06:49 PST
I am adding some code to show the problem. If you run this in IE you
get some spaces between some text; in NS you only get one space.


<html>
<head>


<script language="javascript">
function add_element() {
	var newOption = new Option("the                  text", "the value");
	document.something.drop.options[0] = newOption;
}
</script>

</head>

<form name="something">
<select name="drop">
</select>
</form>

<a href="javascript:add_element();">click here</a>
Answer  
Subject: Re: White space in javascript created form elements under NS
Answered By: hammer-ga on 01 Nov 2002 08:08 PST
Rated:5 out of 5 stars
 
Hi Padders,

You need to get a little tricky to get this past both the HTML
interpreter *and* the JavaScript parser.

1. Place a hidden field on your form.
2. Set the hidden field's value to the number of spaces, using &nbsp;,
that you want to use as a pad
3. Concatenate the hidden field's value into the text you pass to the
new Option call.

Here is sample code illustrating this technique:

<html> 
<head> 
<script language="javascript"> 
function add_element() {
 var new_op;
 new_op = "the" + document.something.whacko.value + "value";
 var newOption = new Option(new_op, "the value");
 document.something.drop.options[0] = newOption; 
} 
</script> 
</head> 
<body>
<form name="something">
<input type="hidden" name="whacko"
value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;">
<select name="drop"> 
</select>
</form> 

<a href="javascript:add_element();">click here</a>
</body>
</html>


I tested this on IE5 and Netscape6. Both handled it properly.

Please post if you need further clarification on this. Good luck!

Hammer

Request for Answer Clarification by padders-ga on 01 Nov 2002 08:18 PST
Hi Hammer,

thanks for the answer. I am going to check if this can fit into my
script (which is a lot more complicated unfortunatly than the example
given) and will get back to you. An interesting approach which I hope
will work.

Clarification of Answer by hammer-ga on 01 Nov 2002 08:47 PST
Thank you for the excellent rating. I hope the technique is adaptable
to your needs.

Hammer
padders-ga rated this answer:5 out of 5 stars and gave an additional tip of: $1.00
Brilliant, answered question perfectly on something i had been unable to work out.

Comments  
Subject: Re: White space in javascript created form elements under NS
From: blader-ga on 01 Nov 2002 05:54 PST
 
Padders:

Have you tried inserting an underscore character and using HTML code
to set the font color of the underscore to match the background color?
I'm not sure if this is feasible without looking at you source, so
this is a comment.

If this works, let me know and I'll post it as an official answer.
Subject: Re: White space in javascript created form elements under NS
From: padders-ga on 01 Nov 2002 06:49 PST
 
HTML code does not work inside an option element. I also don't think
you can use CSS to change the colour of only some elements in an
option and not others.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy