By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,769 Members | 1,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,769 IT Pros & Developers. It's quick & easy.

help! user defined objects in a string...

P: n/a
I have been racking my brain out trying to get this to work... (I am
new to javascript)...

Below is what I am trying to accomplish. I want to have the areas with
+txt+ to have the user defined variable inserted. I can't seem to get
it to work.

<html><head>
<script type="text/javascript">
function color(txt)
{
var check = document.formx.i+txt+.value;
if (check=='') {
d+txt+.style.background='#ffffff';
}
else {
d+txt+.style.background='#c0c0c0';
}
}
</script>
</head><body>

<div id="dv1">
Name of vendor: <input type=text name=iv2 disabled
onkeyup="color(v1)"><BR>
</div>

<div id="dv2">
Name of agent: <input type=text name=iv2 disabled
onkeyup="color(v2)"><BR>
</div>

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
gr********@gmail.com wrote:
I have been racking my brain out trying to get this
to work... (I am new to javascript)...

Below is what I am trying to accomplish. I want to
have the areas with +txt+ to have the user defined
variable inserted. I can't seem to get
it to work. <snip> var check = document.formx.i+txt+.value;

<snip>

Look in the FAQ:-

<URL: http://jibbering.com/faq/#FAQ4_39 >

(<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >)

Richard.
Jul 23 '05 #2

P: n/a

Richard Cornford wrote:
gr********@gmail.com wrote:
I have been racking my brain out trying to get this
to work... (I am new to javascript)...

Below is what I am trying to accomplish. I want to
have the areas with +txt+ to have the user defined
variable inserted. I can't seem to get
it to work.

<snip>
var check = document.formx.i+txt+.value;

<snip>

Look in the FAQ:-

<URL: http://jibbering.com/faq/#FAQ4_39 >

(<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >)

Richard.

Hmm... I tried it like the FAQ said, but still no luck. Would you mind
posting a working version of the code I posted? It keeps saying object
undefined.

Jul 23 '05 #3

P: n/a
On 29/03/2005 22:18, gr********@gmail.com wrote:

[snip]
<URL: http://jibbering.com/faq/faq_notes/square_brackets.html >
Hmm... I tried it like the FAQ said, but still no luck.


Presumably you haven't changed your function calls so that you're
passing string literals not identifiers.

onkeyup="color(v1)"

The script engine will be trying to find - and failing, too - a
variable named v1. Quote it with single quotes.
Would you mind posting a working version of the code I posted?


An example might be in order as there are other issues.

1) You seem to be trying to use the id attribute values of the
two DIV elements as global variables. Don't. That's nonsense
introduced by Microsoft that you'd do well to avoid.
2) Disabling a form control in the mark-up itself is not
something you should do in any environment where you cannot
guarantee script support. Namely, the Web. If you're going to
need a script to enable something, or show something, you
should also use a script to disable or hide that thing in the
first place.
3) You can avoid worrying about the name or id attributes of
elements by passing a reference to the function and using the
DOM to access those element according to their structural
relationship. The example below demonstrates this.

It would seem that you're trying to colour around controls which have
values. A nicer approach than using DIVs would be to use LABELs. If
necessary, you can always make them block-level (using the a "display:
block" declaration in your style sheet) if need be. Also, if you want
some padding underneath the controls, use padding not a forced line break.

function highlight(control) {var colour, label;
/* Control is a reference to the form control.
* If its value is not an empty string, use a
* light grey...
*/
if(control.value) {colour = '#c0c0c0';}
/* ...otherwise use white. */
else {colour = '#ffffff';}

/* If we can obtain a reference to the parent node (the
* LABEL element), and that node has a style object...
*/
if((label = control.parentNode) && label.style) {
/* ...set the background colour to the previously
* determined colour.
*/
label.style.backgroundColor = colour;
}
}
<label>Name of vendor:
<input type="text" name="iv1" onkeyup="highlight(this);">
</label>

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

P: n/a
gr********@gmail.com wrote:
Richard Cornford wrote:
gr********@gmail.com wrote: <snip>
... . I want to
have the areas with +txt+ to have the user defined
variable inserted. I can't seem to get
it to work.<snip>
var check = document.formx.i+txt+.value;

<snip>

Look in the FAQ:-

<URL: http://jibbering.com/faq/#FAQ4_39 >

<snip> Hmm... I tried it like the FAQ said, but still no luck.
Luck is not a factor in computer programming.
Would you mind posting a working version of the code I
posted? It keeps saying object undefined.


That is very unlikely to be what it actually says. But it is in the
nature of computers that specific conditions produce specific error
messages. Those error messages may all seem vague and indistinct at
first but we have seen enough of them to be able to deduce a great deal
from them, given the _exact_ wording (and knowledge of the nature of the
system producing those errors).

That is not how it works. If I post complete code that does what you
appear to want all that will happen is that you will come back looking
for copulate code to solve your next problem. If, on the other hand, we
promote an understanding of whatever you are doing wrong, or
misunderstanding, then you will be in a position to solve your own
problems, and may eventually be in a position to provide assistance to
others.

If you have tried to implement a version of your code that uses
appropriate bracket notation property accessors to allow the dynamic
construction of property names and the result does not work then you
have either erred, or misunderstood. What you now do is post that code
and someone will (may[1]) tell you what you have done wrong, and/or
attempt to explain and correct your misconception(s).

Richard.

[1] It is impossible to guarantee that any post will get a response, and
many shoot themselves in the foot by disregarding Usenet conventions or
failing to fully read the FAQ before asking questions. Both can be
avoided.
Jul 23 '05 #5

P: n/a
Thank you so much for your help! Actually seeing the code helped so
much, because I was able to actually see what works, and use that
information along with what I know about what doesn't work.

Really appreciate it.

Thanks to Richard as well for the words on education within the
community.

Jul 23 '05 #6

P: n/a
Lee
Richard Cornford said:
That is not how it works. If I post complete code that does what you
appear to want all that will happen is that you will come back looking
for copulate code to solve your next problem.


Problem with the spell checker, or some sort of censorship filter?

Jul 23 '05 #7

P: n/a
Lee wrote:
Richard Cornford said:
That is not how it works. If I post complete code that does
what you appear to want all that will happen is that you will
come back looking for copulate code to solve your next problem.


Problem with the spell checker, or some sort of censorship filter?


:) Spell checker. I have corrections while I type turned off on my
user account to prevent Word from doing just that sort of stupid thing
with my otherwise bad spelling and typos, but I was logged on as an
administrator at the time and Word is not configured as I would like
with that account.

Richard.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.