Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-18-2012, 08:54 AM   PM User | #1
resa
New to the CF scene

 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
resa is an unknown quantity at this point
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
resa is offline   Reply With Quote
Old 06-18-2012, 08:17 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,491
Thanks: 18
Thanked 361 Times in 360 Posts
sunfighter is on a distinguished road
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>
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41 AM.


Advertisement
Log in to turn off these ads.