javascript - Populating a text box based on a selection from a drop down -
i tying populate text box based on select box selection.
i have sample code (please see below) , issue : if comment tag , code below working , if uncomment tag not working...could please see code below doing wrong
p.s: need have form since have many other form fields text boxes , select drop downs , need populate text box 1 select.
thanks , appreciate quick response.
here code..
<html> <script type="text/javascript"> function test() { var select = document.getelementbyid("select"); var texts = document.getelementbyid("texts"); var val = select.options.value; texts.innerhtml = ""; if (select.options.value == 2) { texts.innerhtml += 'year:</td><div><td><input type="text" name="test" value="" /></div>'; } } </script> <body> <!--- <form name="myform">---> <select id="select" size="1" onchange="javascript:test();" name ="test"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <hr/> <tr><td> <div id="texts"></div> </td></tr> <!--- </form>---> </body> </html>
the core of issue explained @ select onchange not working inside form.
to explain why name of element takes precedence on function name when inside form due scoping. standard provide access form elements via name part of form object.
for instance in example given access select property window.myform.test
because select inside form when onchange
parsed looks @ local myform
scope find select named test
before window scope function test
declared.
without form test
declared in window scope function.
Comments
Post a Comment