Solved

select list enable/disable and get value

Posted on 2003-11-16
12
4,278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

617 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