What am I doing wrong? js & function()

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

    What am I doing wrong? js & function()

    I want to write a js variable inside a function to a web page.

    test.js

    try_picture();
    function try_picture() {
    var picture = '<tr><td colspan=2 align=right><fo nt size=2>Picture
    &nbsp;&nbsp;5:& nbsp;</font></td><td colspan=2><inpu t type=file size=68
    name="picture5" ></td></tr>';
    }

    HTML

    This is part of a table.

    <script language="javas cript"
    type="text/javascript">doc ument.write(pic ture);</script>

    If I remove the function command, the picture is written into the HTML. The
    function is the problem.

    I have tried try_picture(pic ture) and function try_picture(pic ture). It did
    not work.

    I would appreciate any suggestions.

    I am missing something.

    Ken



  • mscir

    #2
    Re: What am I doing wrong? js &amp; function()

    Ken wrote:[color=blue]
    > try_picture();
    > function try_picture() {
    > var picture = '<tr><td colspan=2 align=right><fo nt size=2>Picture
    > &nbsp;&nbsp;5:& nbsp;</font></td><td colspan=2><inpu t type=file size=68
    > name="picture5" ></td></tr>';
    > }[/color]

    Does this do what you want?

    <head>
    <script type="text/javascript">
    function try_picture() {
    var picture = '<tr>\n<td colspan=2 align=\"right\" >\n<font size=2>';
    picture += 'Picture 5: </font>\n</td>\n<td colspan=2>\n';
    picture += '<input type=\"file\" size=68
    name=\"picture5 \">\n</td>\n</tr>';
    document.write( picture);
    }
    </script>
    </head>

    <body>
    <form>
    <input type="button" onclick="try_pi cture()" value="Try Picture">
    </form>
    </body>

    MIke

    Comment

    • Ken

      #3
      Re: What am I doing wrong? js &amp; function()

      Close, but I want to write the js script on the original html page and not a
      new page.
      form>
      <script language="javas cript"
      type="text/javascript">doc ument.write(pic ture);</script>
      <input type="button" onclick="try_pi cture()" value="Try Picture">
      </form>

      "mscir" <mscir@access4l ess.com.net.org .uk> wrote in message
      news:1083s8qs7m 1lu0a@corp.supe rnews.com...[color=blue]
      > Ken wrote:[color=green]
      > > try_picture();
      > > function try_picture() {
      > > var picture = '<tr><td colspan=2 align=right><fo nt size=2>Picture
      > > &nbsp;&nbsp;5:& nbsp;</font></td><td colspan=2><inpu t type=file size=68
      > > name="picture5" ></td></tr>';
      > > }[/color]
      >
      > Does this do what you want?
      >
      > <head>
      > <script type="text/javascript">
      > function try_picture() {
      > var picture = '<tr>\n<td colspan=2 align=\"right\" >\n<font size=2>';
      > picture += 'Picture 5: </font>\n</td>\n<td colspan=2>\n';
      > picture += '<input type=\"file\" size=68
      > name=\"picture5 \">\n</td>\n</tr>';
      > document.write( picture);
      > }
      > </script>
      > </head>
      >
      > <body>
      > <form>[/color]
      <script language="javas cript"
      type="text/javascript">doc ument.write(pic ture);</script>[color=blue]
      > <input type="button" onclick="try_pi cture()" value="Try Picture">
      > </form>
      > </body>
      >
      > MIke
      >[/color]


      Comment

      • mscir

        #4
        Re: What am I doing wrong? js &amp; function()

        mscir wrote:
        [color=blue]
        > Ken wrote:
        >[color=green]
        >> try_picture();
        >> function try_picture() {
        >> var picture = '<tr><td colspan=2 align=right><fo nt size=2>Picture
        >> &nbsp;&nbsp;5:& nbsp;</font></td><td colspan=2><inpu t type=file size=68
        >> name="picture5" ></td></tr>';
        >> }[/color]
        >[/color]

        Sorry, I goofed, this approach might work:

        <script type="text/javascript">
        function showpic(targetI D) {
        var picture = '<tr><td colspan=2 align=\"right\" ><font size=2>';
        picture += 'Picture 5: </font></td><td colspan=2>';
        picture += '<input type=\"file\" size=68 name=\"picture5 \"></td></tr>';
        document.getEle mentById(target ID).innerHTML=p icture;
        }
        </script>
        </head>

        <body>
        <table width=100 border=1>
        <tr>
        <td><div name="pic1" id="pic1"></div>&nbsp;</td>
        <td>&nbsp;</td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        </tr>
        </table>
        <br>
        <form>
        <input type="button" onclick="showpi c('pic1')" value="Show Pic">
        </form>
        </body>

        Comment

        • Lee

          #5
          Re: What am I doing wrong? js &amp; function()

          Ken said:[color=blue]
          >
          >I want to write a js variable inside a function to a web page.
          >
          >test.js
          >
          >try_picture( );
          >function try_picture() {
          >var picture = '<tr><td colspan=2 align=right><fo nt size=2>Picture
          >&nbsp;&nbsp;5: &nbsp;</font></td><td colspan=2><inpu t type=file size=68
          >name="picture5 "></td></tr>';
          > }
          >
          >HTML
          >
          >This is part of a table.
          >
          ><script language="javas cript"
          >type="text/javascript">doc ument.write(pic ture);</script>
          >
          >If I remove the function command, the picture is written into the HTML. The
          >function is the problem.[/color]

          The problem is that you have declared picture to be a local
          variable of try_picture(). If you want to reference it from
          outside of that function, it must be a global variable.
          The simplest fix is to remove the "var" keyword.

          Comment

          • Grant Wagner

            #6
            Re: What am I doing wrong? js &amp; function()

            Ken wrote:
            [color=blue]
            > I want to write a js variable inside a function to a web page.
            >
            > test.js
            >
            > try_picture();
            > function try_picture() {
            > var picture = '<tr><td colspan=2 align=right><fo nt size=2>Picture
            > &nbsp;&nbsp;5:& nbsp;</font></td><td colspan=2><inpu t type=file size=68
            > name="picture5" ></td></tr>';
            > }
            >
            > HTML
            >
            > This is part of a table.
            >
            > <script language="javas cript"
            > type="text/javascript">doc ument.write(pic ture);</script>
            >
            > If I remove the function command, the picture is written into the HTML. The
            > function is the problem.
            >
            > I have tried try_picture(pic ture) and function try_picture(pic ture). It did
            > not work.
            >
            > I would appreciate any suggestions.
            >
            > I am missing something.
            >
            > Ken[/color]

            function try_picture() {
            // split acorss multiple lines to prevent wrapping
            return '<tr><td colspan=2 align=right>' +
            '<font size=2>Picture& nbsp;&nbsp;5:&n bsp;</font>' +
            '</td><td colspan=2>' +
            '<input type=file size=68 name="picture5" >' +
            '</td></tr>';
            }

            <script type="text/javascript">doc ument.write(try _picture());</script>

            --
            | Grant Wagner <gwagner@agrico reunited.com>

            * Client-side Javascript and Netscape 4 DOM Reference available at:
            *


            * Internet Explorer DOM Reference available at:
            *
            Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


            * Netscape 6/7 DOM Reference available at:
            * http://www.mozilla.org/docs/dom/domref/
            * Tips for upgrading JavaScript for Netscape 7 / Mozilla
            * http://www.mozilla.org/docs/web-deve...upgrade_2.html


            Comment

            Working...