passing a javascript variable into an html form element

Discussion in 'Javascript' started by michael, Nov 14, 2005.

  1. michael

    michael Guest

    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
     
    michael, Nov 14, 2005
    #1
    1. Advertising

  2. michael

    RobG Guest

    michael wrote:
    > 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.



    --
    Rob
     
    RobG, Nov 15, 2005
    #2
    1. Advertising

  3. michael

    michael Guest

    RobG wrote:
    > 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.
    >
    >
    >
    > --
    > Rob


    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
     
    michael, Nov 15, 2005
    #3
  4. michael

    RobG Guest

    michael wrote:
    > RobG wrote:
    >> 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.


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


    >
    >> 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 )?


    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.


    >
    >> 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.


    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.


    --
    Rob
     
    RobG, Nov 15, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod  Snyder

    moving from form element to form element

    Rod Snyder, May 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    409
    Vidar Petursson
    May 29, 2004
  2. HANM
    Replies:
    2
    Views:
    723
    Joseph Kesselman
    Jan 29, 2008
  3. Jonas Daunoravicius
    Replies:
    5
    Views:
    315
    Peter
    Jul 17, 2003
  4. Jonas Daunoravicius
    Replies:
    5
    Views:
    287
    Peter
    Jul 17, 2003
  5. Replies:
    3
    Views:
    245
Loading...

Share This Page