Solved

select list enable/disable and get value

Posted on 2003-11-16
12
4,272 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:sarika1977
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9759982
assuming frmSrcChg is the name of the form you want

    document.frmSrcChg.["cboClsRsn" + i].disabled = false;
0
 

Author Comment

by:sarika1977
ID: 9760069
doesn't work. gives javascript error
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9760112
So post enough info for us to help you proper.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 25

Expert Comment

by:James Rodgers
ID: 9760290
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
 
LVL 25

Expert Comment

by:devic
ID: 9760484
hi sarika1977,
try this:
===========================================
for(var i=1; i <= counter; i++)
{
     eval("frmSrcChg.cboClsRsn"+i+".disabled = false;");
}
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9760595
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
 
LVL 25

Accepted Solution

by:
devic earned 125 total points
ID: 9760868
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9762471
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9762475
sorry the "any idea to make it work using approach 1?"
got left in the code
0
 

Author Comment

by:sarika1977
ID: 9764488
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
 

Author Comment

by:sarika1977
ID: 9764948
devic's approach works for me.
thank you all !!!
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9764991
np -- glad to help


0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question