Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dropdown list and document.getElementById

    Hi,

    I have a form that displays data from the database. In this form I have a field that is a dropdown list:
    Code:
    <div id='subform_grade_udetails0026l332t598' style='position:absolute;top:598px;left:332px'>
                                          <select name="subform_grade_udetails0026udetails_cos" id="subform_grade_udetails0026udetails_cos" class="objects" style="width:80px;" onchange="untick('subform_grade_udetails0026', this);uDB(this);calcStatus();" onfocus="nuSetRow('subform_grade_udetails0026');" >
                                                    <option value=''></option>
                                                    <option value='Failed'>Failed</option>
                                                    <option value='Passed'>Passed</option>
                                            </select>
    
                                       </div>
    Using javascript I'm trying to set the option 'Failed' to "selected".

    I have tried to use:
    Code:
    setSelectedIndex(document.getElementById('subform_grade_udetails0026udetails_cos'),"Failed");
    
    function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
    if ( s.options[i].text == v ) {
    s.options[i].selected = true;
    return;
    }
    }
    }
    It seen that the dropdown box is filled with "Failed". When I save fields are getting blank.
    In the code I see that the "Failed" option is not "Selected" and I don't understand why.

    Could you please help me with this?

    Thank You!
    RESA

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,436
    Thanks
    23
    Thanked 629 Times in 628 Posts
    It's not
    Code:
    if ( s.options[i].text == v )
    It's
    Code:
    if ( s.options[i].value == v )
    really no need to call a function that works as soon as doc is loaded or use onLoad

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    </head>
    <body>
    <div id='subform_grade_udetails0026l332t598'>
      <select name="subform_grade_udetails0026udetails_cos" id="subform_grade_udetails0026udetails_cos" class="objects" style="width:80px;" onchange="untick('subform_grade_udetails0026', this);uDB(this);calcStatus();" onfocus="nuSetRow('subform_grade_udetails0026');" >
                <option value=''></option>
    			<option value="Failed">Failed</option>
                <option value='Passed'>Passed</option>
        </select>
    
    </div>
    
    <script type="text/javascript">
    for (i=0; i<document.getElementById("subform_grade_udetails0026udetails_cos").options.length; i++)
    {
    	if(document.getElementById("subform_grade_udetails0026udetails_cos").options[i].value == 'Passed')
    	{
    		document.getElementById("subform_grade_udetails0026udetails_cos").options[i].selected = 'true';
    	}
    }
    </script>
    </body>
    </html>


  •  

    Posting Permissions

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