Solved

select list enable/disable and get value

Posted on 2003-11-16
12
4,274 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

807 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