Forms without HTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Nolty

    Forms without HTML

    Hi all --

    I'm trying to create a hidden form in the document using only
    Javascript -- I don't know what form I want until the user clicks a
    button --

    I tried the following code, which does create the objects in the
    document hierarchy; the problem is it does not insert the HIDDEN field
    into the list of form elements[].

    <HTML>
    <HEAD>
    <SCRIPT LANG=javascript >
    // recreate the following HTML form:
    // <form method="GET" action="http://search.yahoo.co m/bin/search">
    // <input type="text" name="p" value="" size=35>
    // </form>

    function submitToYahoo() {
    var first = true;
    if (first) {
    alert("creating form");
    var yahooForm = document.create Element("Form") ;
    yahooForm.name = "yForm"
    yahooForm.actio n = "http://search.yahoo.co m/bin/search"
    yahooForm.metho d = "GET";
    yahooFormInput = document.create Element("Hidden ");
    yahooFormInput. name = "p";
    yahooForm.appen dChild(yahooFor mInput);
    document.body.a ppendChild(yaho oForm);

    first = false;
    }
    alert(document. forms.length); // I get 2 here
    alert(document. forms[1].name); // I get "yForm", as expected
    alert(document. forms["yForm"].elements.lengt h); // I get 0, sadly
    alert(document. forms["yForm"].childNodes.len gth); // I get 1, as expected
    alert(document. forms["yForm"].childNodes[0].name); // I get "p", as expected
    document.forms["yForm"].elements[0].value =
    document.forms["theVisible "].input1.value; // runtime error: elements[0] has no properties
    document.forms["yForm"].submit();
    }

    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR=white>
    <form id=theVisible> <input type=text id="input1" size=35 onBlur=submitTo Yahoo()></form>

    </BODY>
    </HTML>

    Any ideas?

    Thanks in advance,
    Bob
  • Lasse Reichstein Nielsen

    #2
    Re: Forms without HTML

    Bob Nolty <nolty@spiritvi deo.com> writes:
    [color=blue]
    > yahooFormInput = document.create Element("Hidden ");[/color]

    You mean
    yahooFormInput = document.create Element("input" );
    yahooFormInput. type = "hidden";
    There is no element called "hidden".

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Bob Nolty

      #3
      Re: Forms without HTML

      Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
      [color=blue]
      > You mean
      > yahooFormInput = document.create Element("input" );
      > yahooFormInput. type = "hidden";
      > There is no element called "hidden".[/color]

      You are right! I was confused because there *is* a class called
      Hidden which inherits from Input (documented in the Client-Side
      JavaScript Reference in O'Reilly's "JavaScript : The Definitive Guide,
      4th Edition"); however, it appears that "Hidden" is not a proper
      argument to createElement (it doesn't throw an error, though :-/ )

      Thanks for getting me back on the right track,
      Bob

      Comment

      • Egbert Beuker

        #4
        Re: Forms without HTML

        what exactly should your page do? the solution seems a little complicated!

        "Bob Nolty" <nolty@spiritvi deo.com> wrote in message
        news:m3n0d8ko6n .fsf@spiritvide o.com...[color=blue]
        > Hi all --
        >
        > I'm trying to create a hidden form in the document using only
        > Javascript -- I don't know what form I want until the user clicks a
        > button --
        >
        > I tried the following code, which does create the objects in the
        > document hierarchy; the problem is it does not insert the HIDDEN field
        > into the list of form elements[].
        >
        > <HTML>
        > <HEAD>
        > <SCRIPT LANG=javascript >
        > // recreate the following HTML form:
        > // <form method="GET" action="http://search.yahoo.co m/bin/search">
        > // <input type="text" name="p" value="" size=35>
        > // </form>
        >
        > function submitToYahoo() {
        > var first = true;
        > if (first) {
        > alert("creating form");
        > var yahooForm = document.create Element("Form") ;
        > yahooForm.name = "yForm"
        > yahooForm.actio n = "http://search.yahoo.co m/bin/search"
        > yahooForm.metho d = "GET";
        > yahooFormInput = document.create Element("Hidden ");
        > yahooFormInput. name = "p";
        > yahooForm.appen dChild(yahooFor mInput);
        > document.body.a ppendChild(yaho oForm);
        >
        > first = false;
        > }
        > alert(document. forms.length); // I get 2 here
        > alert(document. forms[1].name); // I get "yForm", as expected
        > alert(document. forms["yForm"].elements.lengt h); // I get 0, sadly
        > alert(document. forms["yForm"].childNodes.len gth); // I get 1, as[/color]
        expected[color=blue]
        > alert(document. forms["yForm"].childNodes[0].name); // I get "p", as[/color]
        expected[color=blue]
        > document.forms["yForm"].elements[0].value =
        > document.forms["theVisible "].input1.value; // runtime error:[/color]
        elements[0] has no properties[color=blue]
        > document.forms["yForm"].submit();
        > }
        >
        > </SCRIPT>
        > </HEAD>
        > <BODY BGCOLOR=white>
        > <form id=theVisible> <input type=text id="input1" size=35[/color]
        onBlur=submitTo Yahoo()></form>[color=blue]
        >
        > </BODY>
        > </HTML>
        >
        > Any ideas?
        >
        > Thanks in advance,
        > Bob[/color]


        Comment

        • Greg

          #5
          Re: Forms without HTML

          Bob Nolty <nolty@spiritvi deo.com> wrote in message news:<m3ekyjlki 5.fsf@spiritvid eo.com>...[color=blue]
          > Lasse Reichstein Nielsen <lrn@hotpop.com > writes:
          >[color=green]
          > > You mean
          > > yahooFormInput = document.create Element("input" );
          > > yahooFormInput. type = "hidden";
          > > There is no element called "hidden".[/color]
          >
          > You are right! I was confused because there *is* a class called
          > Hidden which inherits from Input (documented in the Client-Side
          > JavaScript Reference in O'Reilly's "JavaScript : The Definitive Guide,
          > 4th Edition"); however, it appears that "Hidden" is not a proper
          > argument to createElement (it doesn't throw an error, though :-/ )
          >
          > Thanks for getting me back on the right track,
          > Bob[/color]

          In my quick test, using IE6, document.forms['form_name'] does not
          return the form. document.forms['form_id'] does return the form.

          FWIW

          Comment

          • Vjekoslav Begovic

            #6
            Re: Forms without HTML

            "Greg" <gdsafford@hotm ail.com> wrote :[color=blue]
            > In my quick test, using IE6, document.forms['form_name'] does not
            > return the form. document.forms['form_id'] does return the form.[/color]

            Quick test indeed, you have to slow down ;-).

            Look at this:

            <form name="fname" id="fid">

            </form>
            <script>
            alert(document. forms['fname'].tagName);
            alert(document. forms['fid'].tagName)
            </script>



            Comment

            • Dom Leonard

              #7
              Re: Forms without HTML

              Vjekoslav Begovic wrote:
              [color=blue]
              > "Greg" <gdsafford@hotm ail.com> wrote :
              >[color=green]
              >>In my quick test, using IE6, document.forms['form_name'] does not
              >>return the form. document.forms['form_id'] does return the form.[/color]
              >
              >
              > Quick test indeed, you have to slow down ;-).
              >
              > Look at this:
              >
              > <form name="fname" id="fid">
              >
              > </form>
              > <script>
              > alert(document. forms['fname'].tagName);
              > alert(document. forms['fid'].tagName)
              > </script>
              >
              >[/color]
              Nice code, but to come up to speed you would need to match
              a) Bob's problem code,
              b) what Greg didn't say :)

              Anyway, /incredible/ as it may seem, there seems to be a bug in IE.

              If a child element of a form is inserted using DOM methods, the child
              must be given an *id* attribute for it to be also made a named property
              of the Form object under IE. It will go into the form's elements
              collection regardless.

              So right after
              <unsnip>
              yahooFormInput. name = "p";
              </unsnip>
              try adding
              yahooFormInput. id = "p";

              Doesn't work for radio input groups unfortunately, but innerHTML can be
              used to programatically insert HTML for these.

              HTH,
              Dom

              ---
              never knowingly make a miss-take.




              Comment

              • Vjekoslav Begovic

                #8
                Re: Forms without HTML

                "Dom Leonard" <doml.removethi s@senet.andthis .com.au> wrote in message
                news:fMf9b.3060 $vQ1.151815@nnr p1.ozemail.com. au...[color=blue]
                > Vjekoslav Begovic wrote:
                >[color=green]
                > > "Greg" <gdsafford@hotm ail.com> wrote :
                > >[color=darkred]
                > >>In my quick test, using IE6, document.forms['form_name'] does not
                > >>return the form. document.forms['form_id'] does return the form.[/color]
                > >
                > >
                > > Quick test indeed, you have to slow down ;-).
                > >
                > > Look at this:
                > >
                > > <form name="fname" id="fid">
                > >
                > > </form>
                > > <script>
                > > alert(document. forms['fname'].tagName);
                > > alert(document. forms['fid'].tagName)
                > > </script>
                > >
                > >[/color]
                > Nice code, but to come up to speed you would need to match
                > a) Bob's problem code,
                > b) what Greg didn't say :)[/color]

                Uppps. I need to be much more careful, sorry Greg.
                [color=blue]
                >So right after
                ><unsnip>
                > yahooFormInput. name = "p";
                ></unsnip>
                >try adding
                > yahooFormInput. id = "p";[/color]

                If the form has the id, that is not nessesary.

                Regards,

                Vjekoslav


                Comment

                Working...