Best way to alternate constraints on two dropdowns - javascript

I have two dropdown selects that have the same values. I am building a converter from the source (left) unit to the destination (right) unit . It doesn't make sense to convert to and from like units. I know how to add an event on a dropdown select to remove the selected option from the alternate box but I am unclear on writing this to produce the best UI experience. Give the following trimmed case:
<select name="srcUnit" id="srcUnit">
<option value="Unit 1">Unit 1</option>
<option value="Unit 2">Unit 2</option>
<option value="Unit 3">Unit 3</option>
</select>
<select name="dstUnit" id="dstUnit">
<option value="Unit 1">Unit 1</option>
<option value="Unit 2">Unit 2</option>
<option value="Unit 3">Unit 3</option>
</select>
What might be a best practice for approaching this? If you choose a source of "Unit 2" how should destination react, remove it? Then I need a reset button, or if you choose "Unit 2" in destination as well, bounce source to something else (that seems worse and intuitive)?
Forget about this and just validate on submit?

Check the new value on each dropdown change event.
If the value is also selected in the other dropdown, move the value of the other dropdown to next (or default) option.
To give the user a clue that he should not select the same value twice, gray out the values that are already select in the other dropdown.
I have written the code for you, here is an executable snippet:
$("#srcUnit, #dstUnit").change(function(){
/*Detect wether we have srcUnit or dstUnit */
if($(this).attr("id") == "srcUnit"){var otherUnit = "dstUnit";}
else{var otherUnit = "srcUnit";}
/*Save new Value*/
var newVal = $(this).children("option:selected" ).val();
/*Select next option in other dropdown if it's old value was selected*/
if(newVal == $("#"+otherUnit+" option:selected").val()){
$('#'+otherUnit+' option:selected').removeAttr('selected').next().attr('selected', 'selected');
}
/*Update UI*/
$('#'+otherUnit+' option').removeClass("disabled");
$('#'+otherUnit+' option[value="'+newVal+'"]').addClass("disabled");
});
.disabled {
color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="srcUnit" id="srcUnit">
<option value="Unit 1" selected>Unit 1</option>
<option value="Unit 2">Unit 2</option>
<option value="Unit 3">Unit 3</option>
</select>
<select name="dstUnit" id="dstUnit">
<option value="Unit 1" class="disabled">Unit 1</option>
<option value="Unit 2" selected>Unit 2</option>
<option value="Unit 3">Unit 3</option>
</select>
I edited my answer to better fit the question, old Answer:
Use the HTML 5 disabled attribute.
<select name="srcUnit" id="srcUnit">
<option value="Unit 1">Unit 1</option>
<option value="Unit 2" selected>Unit 2</option>
<option value="Unit 3">Unit 3</option>
</select>
<select name="dstUnit" id="dstUnit">
<option value="Unit 1">Unit 1</option>
<option value="Unit 2" disabled>Unit 2</option>
<option value="Unit 3">Unit 3</option>
</select>
Read more and try out here: http://www.w3schools.com/tags/att_option_disabled.asp
Subscribe to the onchange event of the dropdowns with javascript and toggle the attribute as needed.

Related

Custom html validation for select

I have a select element like this:
<select name="select">
<option value="opt1">Select One Value Only</option>
<option value="opt2">Type 2</option>
<option value="opt3">Type 3</option>
</select>
and I want user to select a option e.g. opt2, opt3... but opt1,
how to to use html 5 validation to valid the select?
I think the only way to add the validation here is to set the default option value to an empty string and add required attribute to select element. Now you can click on submit button to see the validation:
<form>
<label >Choose an option:</label>
<select name="select" required>
<option value="" disabled selected>Select One Value Only</option>
<option value="opt2">Type 2</option>
<option value="opt3">Type 3</option>
</select>
<input type="submit">
</form>
The issue here is that when you set a value to the default option to something other than empty string the validation infers it as a valid value has been already selected, thus the validation is not triggered at that point.
You can do something like the following:
<select name="select">
<option value="opt1" selected disabled>Select One Value Only</option>
<option value="opt2">Type 2</option>
<option value="opt3">Type 3</option>
</select>
In the snippet above, the opt1 is selected by default. The user can only select opt2 or opt3, but once they do that then they cannot selecte opt1, hope this is the behaviour you're looking for.
Try Using:
<select name="select" required>
<option value="opt1" selected="selected" disabled>Select One Value Only</option>
<option value="opt2">Type 2</option>
<option value="opt3">Type 3</option>
</select>

If <option> is selected deactivate one or more <option> in another dropdown

I am honest, I'm not a Javascript expert. What I want to achieve is to deactivate one or more <option> in the Second Dropdown based on the selection in the First Dropdown
I checked a lot of other answer here but I was not able to adapt them to my code.
<select name="ptp_filter_doc_category" data-column="doc_category" data-tax="doc_category" data-search-column="doc_category_hfilter" aria-label="Category">
<option value="">Category</option>
<option value="category-one">Category 1</option>
<option value="category-two">Category 2</option>
</select>
<select name="ptp_filter_tag_one" data-column="tag_one" data-tax="tag_one" data-search-column="tag_one_hfilter" aria-label="Tag 1">
<option value="">Tag</option>
<option value="tag-1">Tag 1</option>
<option value="tag-2">Tag 2</option>
<option value="tag-3">Tag 3</option>
<option value="tag-4">Tag 4</option>
<option value="tag-5">Tag 5</option>
<option value="tag-6">Tag 6</option>
</select>
What I want to achieve is:
if Category 1 is selected -> Tag 3, Tag 4, Tag 5, Tag 6 are deactivated
I tried this:
if ($('option[value=category-1]').prop('selected', true)) {
$('option[value=tag-3]').prop('disabled', true);
}
Your javascript code will not work because the filtering checks for the wrong value. It should be:
if ($('option[value=category-one]').prop('selected', true)) {
$('option[value=tag-3]').prop('disabled', true);
}
And you may want to use data tags in the tag options to specify for which categories they should be enabled/disabled, so the link between category and tag is not as fixed.

How to redirect user when clicking an option in a select list?

I'm kicking my self for not being able to do this. But I've tried almost everything. I simply want to redirect a user to a specific page when they click an option in my option value list. Here's my current code (which should explain my question better):
<select name="test_redirect">
<option value="1" onclick="document.location = 'http://localhost/shop?item=1';">Item 1</option>
<option value="2" onclick="document.location = 'http://localhost/shop?item=2';">Item 2</option>
<option value="3" onclick="document.location = 'http://localhost/shop?item=3';">Item 3</option>
</select>
I've also tried onChange as well. Same result. Can some one please help me with this? Thank you guys.
document.querySelectorAll("[name=test_redirect]")[0].addEventListener('change',
function () {
window.location = "http://localhost/shop?item=" + this.value;
});
This depends on a relatively new browser (with querySelectorAll and addEventListener), but the principle's the same. click doesn't get triggered on options, so you have to go with change on the <select>. Might as well consolidate the code a bit too.
http://jsfiddle.net/5n5ZE/
<select id="test_redirect">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
javascript
var val = document.getElementById("test_redirect").value;
window.location.href = "http://localhost/shop?item=" + val;
jquery
$("#test_redirect").change(function(){
var val = $(this).val();
window.location.href = "http://localhost/shop?item=" + val;
});
try this (give the link of the page in value of each option)
<select name="test_redirect" onchange="window.location.href=this.form.test_redirect.options[this.form.test_redirect.selectedIndex].value">
You need to bind the event handler to the <select> and not the individual options:
<select name="test_redirect" onchange="location.assign('http://localhost/shop?item=' + this.options[this.selectedIndex].value)">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
This will save you lines :
<select onchange="location = this.options[this.selectedIndex].value;">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
try this
<select name="test_redirect" onchange="location.href='http://localhost/shop?item='+this.value">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>

How do I get input value and Selected Value of dynamically created inputs using JavaScript or JQuery in IE8 and IE9?

I have this working in IE 7 using getElementById() but in IE8 and IE9 that does not work. What I am doing is I need to do validation on the select boxes to see that they are required and check what they selected to do other things. I also need to check the input boxes to see what the value is and it is required as well.
function saveTest(){
var startNumber=0;
var endNumber=3;
// Works
alert(document.myForm.mySelect_1.value);
for (i = startNumber; i <= endNumber; i++) {
// I know this is not right but I am trying everything
alert(document.myForm.mySelect_+i.value);
// Using JQuery to try to solve the issue and this does not work for me.
alert($('#mySelect_'+i).val());
}
}
I know these are not dynamically created belwo but this is just to help me find the answer to this issue. I am creating more selects
<input name="myText_1" id="myText_1" value="">
<br />
<select name="mySelect_1" id="mySelect_1">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<input name="myText_2" id="myText_2" value="">
<br />
<select name="mySelect_2" id="mySelect_2">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
<input name="myText_3" id="myText_3" value="">
<br />
<select name="mySelect_3" id="mySelect_3">
<option value="0">- - - Select One - - -</option>
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
<option value="4">item 4</option>
<option value="5">item 5</option>
</select>
<br />
Save
If I am not clear enough let me know. I will do my best to explain anything you do not understand.
Thanks for your time.
The following line is not correct; you are trying to concatenate a variable name, called mySelect_ to an integer:
alert(document.myForm.mySelect_+i.value);
Instead, you should use the associative array syntax:
alert(document.myForm['mySelect_'+i].value);
Also, within this same loop, you should start from 1 instead of 0, since there's no element called mySelect_0.
Here's a working DEMO.
You have created drop downs with Names
Try using
$('select[name=mySelect_'+i+']').val();
else
$("#id option:selected").text()

html select scroll bar

how do you add a scroll bar to the html select box? is there any javascript library that emulate this behavior?
or is there anyway to redesign, the interface I have 2 select box, items can be shifted to the right select box via >> & << Filezilla, norton commander, total commander kind of interface where items can be shifted left and right... (question is how to redesign to see the overflow words in the fixed width select box)
from this forum post, pointing to another example, the sample code will overlay using div to show the additional text, but it requires the user to hover on the options. not really the solution I am looking for, though I will use it for the time being.
<html>
<body>
<table>
<tr><td>
<select size="4" id="mySelect" style="width:65px;color:#f98ad3;">
<option value="1" selected>option 1 The Long Option</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5 Another Longer than the Long Option ;)</option>
<option value="6">option 6</option>
</select>
</td>
<td>
<input type="button" value=">>"/><br>
<input type="button" value="<<"/>
</td>
<td>
<select size="4" id="mySelect" style="width:65px;color:#f98ad3;">
<option value="1" selected>option 1 The Long Option</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5 Another Longer than the Long Option ;)</option>
<option value="6">option 6</option>
</select>
</td>
</tr>
</table>
</body>
</html>
the target browser is MSIE. from the above code, the user cant see 'option 1 The long Option' etc... and each option supposed to have up to a maximum of 200char.
Horizontal scrollbars in a HTML Select are not natively supported. However, here's a way to create the appearance of a horizontal scrollbar:
1. First create a css class
<style type="text/css">
.scrollable{
overflow: auto;
width: 70px; /* adjust this width depending to amount of text to display */
height: 80px; /* adjust height depending on number of options to display */
border: 1px silver solid;
}
.scrollable select{
border: none;
}
</style>
2. Wrap the SELECT inside a DIV - also, explicitly set the size to the number of options.
<div class="scrollable">
<select size="6" multiple="multiple">
<option value="1" selected>option 1 The Long Option</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5 Another Longer than the Long Option ;)</option>
<option value="6">option 6</option>
</select>
</div>
One options will be to show the selected option above (or below) the select list like following:
HTML
<div id="selText"><span> </span></div><br/>
<select size="4" id="mySelect" style="width:65px;color:#f98ad3;">
<option value="1" selected>option 1 The Long Option</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5 Another Longer than the Long Option ;)</option>
<option value="6">option 6</option>
</select>
JavaScript
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select#mySelect").change(function(){
//$("#selText").html($($(this).children("option:selected")[0]).text());
var txt = $($(this).children("option:selected")[0]).text();
$("<span>" + txt + "<br/></span>").appendTo($("#selText span:last"));
});
});
</script>
PS:- Set height of div#selText otherwise it will keep shifting select list downward.

Categories

Resources