www.webdeveloper.com
Results 1 to 3 of 3

Thread: Can't execute function in div with id? Javascript > Forms > Select, Help!

  1. #1
    Join Date
    Sep 2009
    Posts
    4

    Can't execute function in div with id? Javascript > Forms > Select, Help!

    Can't execute function in div with id? Javascript > Forms > Select, Help!

    I am writing an HTML form. I need the selected value of a <select> tag to then populate an <input type="text" [...] /> tag. I got this to work just fine, but when I wrap the form in a <div id="form">[...]</div> it stops working. I have searched up and down google for an answer, but I am unsure if I even understand how to ask the question!

    Here is my sample code as of yet:

    Code:

    Code:
        <html>
        <head>
        <title>js Test</title>
        <script Language="JavaScript"><!--
        function myTest(){
        var myTest  = form.select.options[form.select.selectedIndex].value;
        alert(myTest);
        }
        </script>
        </head>
        <body>
        <div id="form">
        <form action="" method="post" name="form">
        <span class="title">Add Details</span>
        <ul>
    
        <li>
        <label>Label</label>
        <span class="note">Note</span>
        <select name="select" onChange="myTest()">
        <option value=""></option>
        <option value="this text in alert box">trying</option>
        <option value="some other text">something</option>
        <option value="value 3">new</option>
        </select>
        </li>
        </ul>
        </form>
        </div>
    
        </body>
        </html>
    So if you remove the id="form" from <div id="form"> it works just fine and gives me an alert.

    How to I reference the <div id=form> to make it work properly?
    Do I need to do this for every div the form is in?

    I have done PHP for a number of years but not javascript, so this is kind of new to me.


    Thanks in advance!

  2. #2
    Join Date
    Jan 2009
    Location
    Sao Paulo, Brazil
    Posts
    15
    Try this code:

    Code:
    function myTest(){
    	var myTest  = document.form.select.options[document.form.select.selectedIndex].value;
    	alert(myTest);
    }
    Add document in the code.

  3. #3
    Join Date
    Sep 2009
    Posts
    4

    Almost there, a few more questions

    Ok, That seams to have worked in my quick crude sample, but doesn't seam to work for the actual page that I am trying this on. Thanks for the help!

    I think I understand what that "document.whatever..." line does, but I can't seams to figure it out. I am a long time PHP programmer, but I never understood Java and never really coded it until today. So as you can image, a lot of questions come to mind. Could you maybe explain how that "document.something.form.whatever.value" is structured? What is it called so I can good look it up myself.

    I'm sure it's something simple I just don't know what to search for to find my answer!

    Thanks again for the help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles