SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    use javascript when click select box

    I have select box like this
    Code:
    SCRIPT LANGUAGE="JavaScript">
    function choose()
    {
    if (document.all.select1.value=="1")
    {
    mypic.src="newsletter_traditional.gif";
    }
    if (document.all.select1.value=="2")
    {
    mypic.src="newsletter_modern.gif";
    }
    }
    </SCRIPT>
    
    <select size=6 name="select1" onchange="choose()" width=100>
    <option value="1" selected>Traditional
    <option value="2">Modern
    </select>
    <img src="newsletter_traditional.gif" name="mypic" border=0>
    This code work. when I click each option. it display own image.

    The problem is when I add tag

    <form action="go.php" method="post">
    and
    </form>

    When I run again. it has error

    I try to assign form name name="form1" and in function choose() I change from

    document.all.select1.value
    to
    document.form1.select1.value
    or
    form1.select1.value

    but it still has error. It tell that undefine mypic vairable. I don't know how to fix it. Please help me.
    I live in Thailand. My English grammar not well.

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    Code:
    <html>
      <head>
        <script language="javascript">
          function choose(value)
          {
            var mypic = document.getElementById("mypic");
            if(value == 1)
            {
              mypic.src = "newsletter_traditional.gif";
            }
            else
            {
              mypic.src = "newsletter_modern.gif";
            }
          }
        </script>
      </head>
      <body>
        <form>
          <select size=6 name="select1" onchange="choose(this.value)" width=100>
            <option value="1" selected>Traditional
            <option value="2">Modern
          </select>
        </form>
        <img src="newsletter_traditional.gif" id="mypic" border=0>
      </body>
    </html>

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Warning: document.getElementById("mypic") will not work in Netscape 4
    If you expect users to have NS4, then use document.images[0] instead (where 0 can be different if the img isn't the first image in the document)

  4. #4
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Is any wat to write code that work both IE and Netscape.
    Last edited by itsource; Nov 7, 2002 at 01:31.
    I live in Thailand. My English grammar not well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •