Jump to content

Dynamic Drop Down Box


Lucky2710

Recommended Posts

I have a form with 3 drop down boxes (all exactly the same) but i don't want users to be able to select the same thing twice so how can i make it so that when they select something in on box that it either becomes grayed out or disappears all together from the other drop down boxed?

 

I think I'm going to have to use php if else but before i write the code i wanted to make sure thats the way to do it. (Each drop down box has bout 75 option to choose from)

 

Any thoughts or suggestions?

Link to comment
Share on other sites

Well to do it on the page itself, you're looking more at JavaScript/JQuery.  If you want to do it after the submit, I'd suggest something like this:

if($_POST['box1'] === $_POST['box2'] || $_POST['box1'] === $_POST['box3'] || $_POST['box2'] === $_POST['box3']){
    die('You cannot make the same selection twice');
}

Link to comment
Share on other sites

There is a disabled attribute for the <option > tag - http://w3schools.com/tags/att_option_disabled.asp

 

You can easily come up with some javascript to set that attribute whenever a choice is made on an earlier <select> menu.

 

And since this is a client side problem, moving thread the Javascript forum section ...

 

Link to comment
Share on other sites

Here is a specific example of using an id that would probably be helpful - http://www.w3schools.com/js/tryit.asp?filename=try_dom_option_disabled_id

 

If the <option that is selected has an id that relates it to the id of the option that should be disabled, it should be fairly easy to write some generic code to do this. Something like id = 'opt1a' and id 'opt1b'. If option opt1a is the id that gets selected, you can set the disabled attribute for id = 'opt1b'.

Link to comment
Share on other sites

The following demonstrates basically how to do this (assuming you are not using IE browsers) -

<script type="text/javascript">
function disableselected(selectobj){
var nextselect=document.getElementById("sel2"); // reference the next select menu
nextselect.options[selectobj.selectedIndex].disabled=true; // disable the same element in the next select that was just selected in the first select
}
</script>

<form>
<select onChange="disableselected(this)">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<select id="sel2">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</form>

 

The above code leaves any items disabled, so if you change your choice in the first drop-down, each corresponding item in the second drop-down will become disabled. To correct this you would need to loop through all the elements in the second drop-down and set the .disabled element = false before you set the newly selected one to true.

Link to comment
Share on other sites

Dude this is exactly what i wanted.

The following demonstrates basically how to do this (assuming you are not using IE browsers) -

Code: [select]

<script type="text/javascript">

function disableselected(selectobj){

var nextselect=document.getElementById("sel2"); // reference the next select menu

nextselect.options[selectobj.selectedIndex].disabled=true; // disable the same element in the next select that was just selected in the first select

}

</script>

 

<form>

<select onChange="disableselected(this)">

<option>Make Your Pick</option>

<option>Option 1</option>

<option>Option 2</option>

<option>Option 3</option>

</select>

<select id="sel2">

<option>Make Your Pick</option>

<option>Option 1</option>

<option>Option 2</option>

<option>Option 3</option>

</select>

</form>

 

The above code leaves any items disabled, so if you change your choice in the first drop-down, each corresponding item in the second drop-down will become disabled. To correct this you would need to loop through all the elements in the second drop-down and set the .disabled element = false before you set the newly selected one to true.

 

Thats almost exactly what i need. ( i just need three drop downs. Bu t i think i can figure that out if not i know who to ask.

 

Thanks

 

Link to comment
Share on other sites

K I'm so close. I've got it to once the first selection is made that the second box has 1st disabled then once you make your second pick it disables that one in the third box. But i just cant seem to make the first selection carry over past the second box to the third!

<script type="text/javascript">
function disableselected(selectobj){
var nextselect=document.getElementById("sel2"); // reference the next select menu
nextselect.options[selectobj.selectedIndex].disabled=true; // disable the same element in the next select that was just selected in the first select
}
function disableselected2(selectobj){
var nextselect=document.getElementById("sel3"); // reference the next select menu
nextselect.options[selectobj.selectedIndex].disabled=true; // disable the same element in the next select that was just selected in the first select
}
</script>

<form>
<select onChange="disableselected(this)">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<select id="sel2" onChange="disableselected2(this)">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<select id="sel3">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</form>

 

 

Thanks again to PFMaBiSmAd for getting me this far.

Link to comment
Share on other sites

Give this a try (note: using disabled for an <option> tag works in browser other than IE and in IE8) -

<script type="text/javascript">
function disableselected(selectobj){
var select2=document.getElementById("sel2");
var select3=document.getElementById("sel3");
// if the first choice is changed, reset the 2nd and 3rd select lists
for (var i=0; i<selectobj.options.length; i++){
	select2.options[i].disabled=false; // clear any exiting disabled options
	select3.options[i].disabled=false; // clear any exiting disabled options		
}
select2.options[0].selected=true; // clear any exiting selection (select the Make Your Pick option)
select3.options[0].selected=true; // clear any exiting selection (select the Make Your Pick option)
// if you didn't pick the Make Your Pick, disable the same choice in the 2nd and 3rd select lists
if(selectobj.selectedIndex != 0){
	select2.options[selectobj.selectedIndex].disabled=true;
	select3.options[selectobj.selectedIndex].disabled=true;
}
}

function disableselected2(selectobj){
var select3=document.getElementById("sel3");
// if the second choice is changed, reset the 3rd select list
for (var i=0; i<selectobj.options.length; i++){
	if(selectobj.options[i].disabled == false){
		select3.options[i].disabled=false; // clear any exiting disabled options		
	} else {
		select3.options[i].disabled=true; // set disabled if the current menu's option is also disabled	
	}
}
select3.options[0].selected=true; // clear any exiting selection (select the Make Your Pick option)
// if you didn't pick the Make Your Pick, disable the same choice in the 3rd select list
if(selectobj.selectedIndex != 0){
	select3.options[selectobj.selectedIndex].disabled=true;
}
}
</script>

<form>
<select onclick="disableselected(this)">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<select id="sel2" onclick="disableselected2(this)">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<select id="sel3">
<option>Make Your Pick</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.