PDA

View Full Version : Problems with javascript syntax


dhtmlhelp
08-14-2002, 12:18 AM
Hi,

can anyone explain me how to write:

onFocus="if(this.value=='Hello')this.value=''; select()"

inside:

function random_content(){
var mycontent=new Array()
mycontent[1]=' '

I am having problems with the ' '

what is an acceptable substitute?

DHTMLHELP

WA
08-14-2002, 12:27 AM
dhtmlhelp:
The code

onFocus="if(this.value=='Hello')this.value=''; select()"

is an event handler, and I really can't see any reason why you would want to add it inside an array element. It just doesn't make sense (no offense).

Maybe if you could describe what you're trying to accomplish, people can help out more. In general, it's not even possible to embed an event handler into an array element in the manner you're asking.

dhtmlhelp
08-14-2002, 02:19 AM
Hi WA,

no offense at all, I just need to do what I have written above. is there anyway I can apply the onfocus event handler to the array element with css using class="whatever"? (if yes how would I need to write it in css?)

DHTMLHELP

WA
08-14-2002, 02:52 AM
Event handlers must be associated with a particular HTML tag, not a JavaScript variable. For example, the below is a valid use of the onFocus event handler:

<textarea onFocus="if(this.value=='Hello')this.value=''; select()"></textarea>

What you're asking is like "How do I add a table inside an image? The two simply aren't supposed to- nor would you want to- fit together.

dhtmlhelp
08-14-2002, 10:01 AM
Hi,

why do you insist I wouldn't want to. I need to display random forms with the javascript I have included above. The form goes:

<form etc..><input type="text" etc... onFocus="if(this.value=='Hello')this.value=''; select()"> and I need to put this inside the javascript to be able to display it randomly (more specifically inside mycontent[1]=' '.


Now you say I cannot include event handlers inside javascript. In that case can I use css to get around the problem, and if yes could you explain me how?

DHTMLHELP

glenngv
08-14-2002, 10:12 AM
use the backslash operator. to include double quotes in a string, use \"

mycontent[1]="onFocus=\"if(this.value=='Hello')this.value=''; select()\""

dhtmlhelp
08-14-2002, 10:38 AM
works great thanks

DHTMLHELP