passing a javascript variable into an html form element

M

michael

let me keep it clean, quick and simple.

I am passing a variable into another window and am reassigning the
value on the new page -

window.document.[formNameA].[varibaleNameA].value = opener.document.
..[formNameB].[varibaleNameB].value

and am wanting to then use this value within an element of a form on
the current page -

<form name="[formNameA]">

<input name="[variableNameA]" type="text">

</form>

but cannot seem to get the value passed to actually show up within the
input field of the form.

any thoughts or suggests?

t.i.a.




michael
 
R

RobG

michael said:
let me keep it clean, quick and simple.

Good intention, but you've left out too much.
I am passing a variable into another window and am reassigning the
value on the new page -

window.document.[formNameA].[varibaleNameA].value = opener.document.
.[formNameB].[varibaleNameB].value

Presumably you are opening a popup from the window containing
'formNameB' and hoping to write the value of 'varibaleNameB' to the
popup's form named 'formNameA' with an element named 'varibaleNameA'.

Part of the problem is that the syntax is wrong, something like:

document.formNameA.varibaleNameA.value =
opener.document.formNameB.varibaleNameB.value;


would do a better job - note the spelling of 'varibale' (sic).

and am wanting to then use this value within an element of a form on
the current page -

<form name="[formNameA]">

<input name="[variableNameA]" type="text">

Your call is to 'varibaleNameA', note the difference in spelling.
</form>

but cannot seem to get the value passed to actually show up within the
input field of the form.

If you are using document.write to write the content of the popup, then
likely formNameA doesn't exist when you try to write to it.
any thoughts or suggests?

Showing the code (small test example) of what you are actually trying to
do would help.

Here's a small sample:


<script type="text/javascript">

function popForm()
{
var newWin = window.open('','New_Window','');
newWin.document.write(
'<title>The new window</title>',
'<form name="formNameA" action="">',
'<input type="text" name="variableNameA" value="',
document.formNameB.variableNameB.value,
'"></form>'
);
newWin.document.close();
}
</script>
<form action="" name="formNameB">
<input type="text" name="variableNameB" value="blah">
<input type="button" value="Pop form" onclick="popForm()">
</form>


It would be better to pass a reference to the form from the onclick
event, but for the sake of the exercise I have kept the syntax close to
your original.
 
M

michael

RobG said:
Good intention, but you've left out too much.

yeah, simple AND informative, not a good combination for me.
Presumably you are opening a popup from the window containing
'formNameB' and hoping to write the value of 'varibaleNameB' to the
popup's form named 'formNameA' with an element named 'varibaleNameA'.

Part of the problem is that the syntax is wrong, something like:

document.formNameA.varibaleNameA.value =
opener.document.formNameB.varibaleNameB.value;


would do a better job - note the spelling of 'varibale' (sic).

at least my misspellings were consistent, eh? well, at least until I
called the variable to rename it...

for the sake of this exercise, I'm simply opening another window, but
the popup was the original intent.
Showing the code (small test example) of what you are actually trying to
do would help.

permission to email my test files ( four in all )?
It would be better to pass a reference to the form from the onclick
event, but for the sake of the exercise I have kept the syntax close to
your original.

uhm, yeah, back to your opening remark,
Good intention, but you've left out too much.

one of my objectives is to have the page(s) automatically move the
end-user through the pages after making their selections and or text
inputs. a tad more complication to the mix ( or what I like to call
confused state of uhm-dom ).

Rob,

I appreciate your taking the time and look forward to any future
responses. and apologies for not succinctly providing the necessary
details.



michael
 
R

RobG

michael said:
yeah, simple AND informative, not a good combination for me.


at least my misspellings were consistent, eh? well, at least until I
called the variable to rename it...

for the sake of this exercise, I'm simply opening another window, but
the popup was the original intent.

If your script doesn't open the new window (AKA a popup), it can't
access it at all.

permission to email my test files ( four in all )?

No, but there's no harm in posting up to say 100 lines of code that
display the issue. Often in developing a test case for posting you'll
discover a solution. If not, you have likely learned something anyway.

uhm, yeah, back to your opening remark,

When an intrinsic event is fired you can get a reference to the element
that fired it using 'this', e.g.:


<input type="button" name="button-01" onclick="showName(this);">

<script type="text/javascript">
function showName(el)
{
// If the element has a name, show it
if (el.name) alert('The name is ' + el.name);

// If the element is in a form, say so
if (el.form) {
alert('I\'m in a form');
} else {
alert('Hangin\'...');
}
}
</script>

Every element that is a form control has a 'form' property that is a
reference to the form the element is in. So once you have a reference
to the element with 'this' it is trivial to get a reference to the form.

Alternatively you could climb the DOM tree of the element's ancestors
until you get to a form element but it's not required (though that
strategy can be employed in other situations like getting a reference to
the table that a cell is in).


If an event is fired by a form's onsubmit handler:

<form name="formA" onsubmit="return doStuff(this);" ... >


Then doStuff can get a reference to the form:

function doStuff(theForm)
{
// theForm is a reference to formA
}

one of my objectives is to have the page(s) automatically move the
end-user through the pages after making their selections and or text
inputs. a tad more complication to the mix ( or what I like to call
confused state of uhm-dom ).

I take it that you hope to open windows to let the user make selections
that are then put into the form.

As a general strategy, that is fine but remember that not everyone has
JavaScript enabled so successful completion of the form should not be
dependent on JavaScript. The non-scripted version need not be pretty as
long as it is functional.

If this is for an intranet, then maybe you can ignore that advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top