www.webdeveloper.com
Results 1 to 2 of 2

Thread: javascript alert

  1. #1
    Join Date
    Nov 2004
    Location
    xxxyyy
    Posts
    357

    javascript alert

    How do I get the value from a field which is dynamically added to the page ?

    see the following code

    Steps to reproduce error message

    step1: run this html in IE
    step 2: click INSERT
    step3: select 'Zero' from the select box' // this option has been dynamically added ! (but not present in view source)

    This throws alert error message . How do I get the value from a field which is dynamically added to the page then?

    whats the fix ?

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function insertOption() {
    anOption = document.createElement("option") 
    document.myform.time.options.add(anOption,3) 
    anOption.innerText = "Zero"; //The text for the option
    anOption.Value = "Zero"; //The option's value
    document.myform.time.options.selectedIndex = 0;
    }
    
    function call() {
    alert('hi');
    
    alert(document.myform.time.value); //  when I select 'Zero'  ..it does not print 'Zero' but throws error. how to fix this ?
    
    }
    </script>
    
    </head>
    <body>
    
    <form name="myform">
    <select name="time" size="1" onchange="call()">
    <option value="One">One</option> 
    <option value="Two">Two</option> 
    <option value="Three">Three</option> 
    </select> 
    <input type="button" name="now" value="Insert" onclick="insertOption();" >
    </form>
    </body>
    
    </html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    anOption.text = "Zero"; //The text for the option
    anOption.value = "Zero"; //The option's value

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