Solved

Drop Down Fields disabling in JSP

Posted on 2010-09-15
4
718 Views
Last Modified: 2012-05-10
I have certain fields like 9 of them in a javascript dropdown.

In a specific scenario i need to have only one field in the dropdown and rest i shouldnt see them

I am new to this and need a sample example to solve this.
0
Comment
Question by:ashokvarma06
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33688685
I am not sure if you can hide the options, but can disable them
This code will disable all but first and third option






<script>
	function showOnlyFirstOne()
	{
		var alloptions = document.getElementById("dd").options;
		alert(alloptions.length);
		for ( var counter = 0; counter < alloptions.length; counter++ )
		{
			alloptions[ counter ].disabled=true;
		}
		alloptions[ 0 ].disabled=false;
		alloptions[ 2 ].disabled=false;
		alert("all blocked");
	}
</script>
<body onload="showOnlyFirstOne()">
	<SELECT id="dd">
		<OPTION value="1">1</OPTION>
		<OPTION value="2">2</OPTION>
		<OPTION value="3">3</OPTION>
		<OPTION value="4">4</OPTION>
		<OPTION value="5">5</OPTION>
		<OPTION value="6">6</OPTION>
	</SELECT>
</body>

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33688690
the name of the method is misleading :), please change the same to disableOption()
0
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33688891
If you want it from JSP then you can hide them from the jsp code only. Only required fields will be rendered then. an example is as below

<select>
<%
boolean set1=true;
boolean set2=true;
if(set1){out.println("<option value='1'>1</option>");}
if(set1){out.println("<option value='2'>2</option>");}
if(set1){out.println("<option value='3'>3</option>");}
if(set2){out.println("<option value='a'>A</option>");}
if(set2){out.println("<option value='b'>B</option>");}
if(set2){out.println("<option value='c'>C</option>");}
%>
</select>

here set1 and set2 are the two boolean variables which can be set or unset. i have set them to true in the above case.

from javascript you can remove them using removeChild function eg
<select id="test">
<option id='1' value='1'>1</option>
<option id='2' value='2'>2</option>
<option id='3' value='3'>3</option>
<option id='4' value='a'>A</option>
<option id='5' value='b'>B</option>
<option id='6' value='c'>C</option>
</select>

<script type="text/javascript">
    var d=document.getElementById('test');
    d.removeChild(document.getElementById("1"));
</script>
0
 
LVL 1

Expert Comment

by:su_mu
ID: 33690077
You can store the drop down values inside a array. Based on the specific scenario, you can populate the drop down dynamically.

E.g. Consider this HTML in your page
<span id="spa"><select id="name"><option>Select One</option>
</select></span>
<button name="butt" onClick="changeText()">Button Click</button>

<script>
var values = new Array(5);
// At index [1] you can store a flag for the conditional load
values[0] = new Array('1','0');
values[1] = new Array('2','1');
values[2] = new Array('3','1');
values[3] = new Array('4','1');
values[4] = new Array('5','0');

function changeText(){
      str = "<select id='name'>";
      // For 1st Cond
      cond = '1';
      for(i=0;i<5;i++){
      if(values[i][1] == cond){
      str = str+"<option value='"+values[i][0]+"'>"+values[i][0]+"</option>";
      }
      }
      str = str+"</select>";
      document.getElementById("spa").innerHTML = str;
}
</script>

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stateless EJB with glassfish 2 73
using if condition without JSTL 2 109
memory representation of int[][][] a=new int[2][][]; 10 60
runtime exception and error differences 4 81
In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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