Solved

select list enable/disable and get value

Posted on 2003-11-16
12
4,269 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now