select list enable/disable and get value

i am facing similar problem at 2 places.

i have disabled some select boxes, and trying to enable them through program.            

for(var i=1; i <= counter; i++)
{
      // approach (1) does not work
      frmSrcChg.all["cboClsRsn" + i].disabled = false;
     
      // approach (2) works
      frmSrcChg.cboClsRsn1.disabled = false;
      frmSrcChg.cboClsRsn2.disabled = false;
      frmSrcChg.cboClsRsn3.disabled = false;
      frmSrcChg.cboClsRsn4.disabled = false;
}
any idea to make it work using approach 1?

similarly in another loop where i try to get value of selected item from list

var p = frmSrcChg.all["cboClsRsn" + i].options[frmSrcChg.all["cboClsRsn" + i].selectedIndex].text;

looks like javascript treats it like a string so

  alert(p)

prints a string and not the value.

thanks
sarika1977Asked:
Who is Participating?
 
devicCommented:
here both version in action:
============================
<html>
<head>
      <title>Untitled</title>
      <META http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>

<body>
<script>
function runit(frmSrcChg,Statusdisabled)
{
      for(var i=1;i<5;i++)
      {
      eval("frmSrcChg.cboClsRsn"+i+".disabled = "+Statusdisabled+";");
      }
}
function runit2(frmSrcChg,Statusdisabled)
{
      for(var i=1;i<5;i++)
      {
      frmSrcChg["cboClsRsn"+i].disabled = Statusdisabled;
      }
}
</script>

<form action=http://www.google.com/search>
<select name=cboClsRsn1 disabled>
      <option value=aaaa>aaaa
</select>
<select name=cboClsRsn2 disabled>
      <option value=aaaa>aaaa
</select>

<select name=cboClsRsn3 disabled>
      <option value=aaaa>aaaa
</select>

<select name=cboClsRsn4 disabled>
      <option value=aaaa>aaaa
</select>
<br><br>
eval("frmSrcChg.cboClsRsn"+i+".disabled = "+Statusdisabled+";");<br>
<input type=button onclick=runit(this.form,false) value=enable>
<input type=button onclick=runit(this.form,true) value=disable>
<br>
<hr>
<br>
frmSrcChg["cboClsRsn"+i].disabled = Statusdisabled;<br>
<input type=button onclick=runit2(this.form,false) value=enable>
<input type=button onclick=runit2(this.form,true) value=disable>
</form>

</body>
</html>
0
 
Michel PlungjanIT ExpertCommented:
assuming frmSrcChg is the name of the form you want

    document.frmSrcChg.["cboClsRsn" + i].disabled = false;
0
 
sarika1977Author Commented:
doesn't work. gives javascript error
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Michel PlungjanIT ExpertCommented:
So post enough info for us to help you proper.
0
 
James RodgersWeb Applications DeveloperCommented:
how about

for(var i=1; i <= counter; i++)
{
 strObjName="cboClsRsn"+i;

 document.getElementById(strObjName).disabled=false;

}

this will only work if i is not greater than the number of elements with the name
cboClsRsnX

0
 
devicCommented:
hi sarika1977,
try this:
===========================================
for(var i=1; i <= counter; i++)
{
     eval("frmSrcChg.cboClsRsn"+i+".disabled = false;");
}
0
 
JakobACommented:
if  this works:
      // approach (2) works
      frmSrcChg.cboClsRsn1.disabled = false;
 then the below will also work
      // approach (3) works
      frmSrcChg["cboClsRsn"+1].disabled = false;

regards JakobA
 
0
 
Michel PlungjanIT ExpertCommented:
What does javascript mingled with html do???

If you have

<form name="frmSrcChg">
<select name="cboClsRsn1">
<option value="SEL1VALUE0">SEL1TEXT0
<option value="SEL1VALUE1">SEL1TEXT1
</select>
<select name="cboClsRsn2">
<option value="SEL2VALUE0">SEL2TEXT0
<option value="SEL2VALUE1">SEL2TEXT1
</select>
.
.
.
then the code you need is

counter = 4
for(var i=1; i <= counter; i++) {
  document.frmSrcChg.elements["cboClsRsn" + i].disabled = false;
}
any idea to make it work using approach 1?

and later

var p = document.frmSrcChg.elements["cboClsRsn" + i].options[document.frmSrcChg.elements["cboClsRsn" + i].selectedIndex].value; // VALUE not TEXT

you can shortcut this like
  sel = document.frmSrcChg.elements["cboClsRsn" + i];
var p = sel.options[sel.selectedIndex].value; // VALUE not TEXT (unless you want what is shown in the select rather than what you put as value)
0
 
Michel PlungjanIT ExpertCommented:
sorry the "any idea to make it work using approach 1?"
got left in the code
0
 
sarika1977Author Commented:
code:


<FORM name="frmSrcChg" id="frmSrcChg" action="" method="post" style="cursor:default;">
....
....
<td width="10%"><select size="1"  name="cboClsRsn1">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
.....
<option value="18">18</option>
<option value="19">19</option></select></td>
</tr>
<tr>
<td width="90%" style="padding-left:15px">Reason 2 </td>
<td width="10%"><select size="1" name="cboClsRsn2">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
...
<option value="18">18</option>
<option value="19">19</option></select></td>
</tr>
<tr>
<td width="90%" style="padding-left:15px">Reason3</td>
<td width="10%"><select size="1" name="cboClsRsn3">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
...
<option value="18">18</option>
<option value="19">19</option></select></td>
</tr>
...
...
0
 
sarika1977Author Commented:
devic's approach works for me.
thank you all !!!
0
 
James RodgersWeb Applications DeveloperCommented:
np -- glad to help


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.