I have a web form that I'm applying both client and server side validation to and on this one text input field I'm looking for some way to have conditional code that sets a character limit based on what a dropdown menu is set to.
For example:
If Option 3 is selected, limit maximum character limit to 10.
<select>
<option>Option 1</option>
<option> Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select>
<label>Text Field</label>
<input type="text" />
I've been experimenting with a number of pre-coded javascript / jquery validation scripts and they all work fine except when it comes to this particular need.
How can this be done?
Thanks for any advice.
Create a select onchange event that will set the maxLength of the input. You can use the option value as the value for the event. Example:
<select id="sel" onchange="document.getElementById('txt').maxLength = document.getElementById('sel').value;" >
<option value="3">Option 1</option>
<option value="5"> Option 2</option>
<option value="10">Option 3</option>
<option value="20">Option 4</option>
</select>
<br/>
<label>Text Field</label>
<input type="text" id="txt" value="" maxlength="3" />
You can test it here - http://jsfiddle.net/codefuze/R7RtJ/
Related
I need to make an analog of the size attribute in the Select2 plugin as in the default select. I can't find it in the docks.
Here's how implemented in default
Sorry for English :)
In html you can use disabled ether on the entire selection or option as showing in your image, here is an example :
<select>
<option value="1" disabled>option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4" disabled>option 4</option>
</select>
as you can see you can't select option 1 and 4
I have a form that has several dropdowns. I need to make the second dependent on the value of the first. I have a Codepen that shows four dropdowns. The first does not change but the selects 2, 3, and 4 show the intended options I want.
Here is a listing of what option of dropdown 1 leads to the second.
new -> input-2
client -> input-3
admin -> input-4
I am doing this in OpenCart in a MVC so I am a little confused if this all is done in the view file or do I need to have some of this done in the controller, and if so how? I do believe JavaScript would be able to help here but not really sure how to implement that. My code pen is:
https://codepen.io/anon_guy/pen/pdbYad
<form action="/action_page.php">
<select name="Input-1">
<option value="*"></option>
<option value="new">new</option>
<option value="client">client</option>
<option value="admin">admin</option>
</select>
<select name="Input-2">
<option value="sign-up">Sign Up</option>
</select>
<select name="Input-3">
<option value="upgrade">upgrade</option>
<option value="stats">stats</option>
</select>
<select name="Input-4">
<option value="view-accounts">view all</option>
<option value="add">add</option>
<option value="remove">remove</option>
</select>
<input type="submit">
</form>
This question already has answers here:
How to display only the text in datalist HTML5 and not the value?
(3 answers)
Closed 5 years ago.
I want to hide datalist value. Am using this code
<input list="options" oninput="console.log(this.value);" />
<datalist id="options">
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
</datalist>
I mean in the inputbox this showing values and product 1
i need to show only product 1,2,&3...
hide value (1,2,3)
.
Note: Here am passing value 1,2,3 so i just want to hide that only
here my code https://jsfiddle.net/69u5Leoa/
You may want to use select, because for the select element, the user is required to select one of the options you've given.
For the datalist element, it is suggested that the user select one of the options you've given, but he can actually enter anything he wants in the input.
you can see this to get more information HTML Form: Select-Option vs Datalist-Option
<select id="options">
<option></option>
<option value="1">Product 1</option>
<option value="2">Product 2</option>
<option value="3">Product 3</option>
</select>
Value="<%# DataBinder.Eval(Container, "ItemIndex") %>" it can solution this problem
EDIT:
I forgot http:// on the links. This is not my problem. I'll fix the question...
i have some options, like this:
<select class="mySelect">
<option value="http://www.url1.com">Url 1</option> <!--Default displayed--->
<option value="http://www.url2.com">Url 2</option>
<option value="http://www.url3.com">Url 3</option>
</select>
To this i have a jquery listener, looks like this:
$('.mySelect').change(function(){
window.location.replace($(this).val());
});
Since this just responds to a change made, i can't be redirected to "www.url1.com". What should i use? I only get suggestions for change on google.
EDIT 2: I've got a bunch of solutions on using a dummy as first option. I also got the other solution of using a button to read the selected option and then redirect, but that's not usable in the project i'm working on, even if these are really good solutions otherwise.
In this case, i want it to listen for usage, even if the change is made to itself, like i'm selecting the first option (that's selected by default) i want it to do something with it - In the case above a redirect. I'll leave it open if someone have a good solution to this problem.
Try putting in an empty valued first option:
<select class="mySelect">
<option value="">Please select...</option>
<option value="http://www.url1.com">Url 1</option>
<option value="http://www.url2.com">Url 2</option>
<option value="http://www.url3.com">Url 3</option>
</select>
You can then amend your jQuery to make sure there is a value before redirecting:
$('.mySelect').change(function(){
var value = $(this).val();
if (value != "") {
window.location.replace(value);
}
});
Example fiddle
Note also that http:// (or whichever protocol you're using) is required on the link values.
Updated
If you would prefer to not have the dummy option, you would need to change the logic to work on button click:
<select class="mySelect">
<option value="http://www.url1.com">Url 1</option>
<option value="http://www.url2.com">Url 2</option>
<option value="http://www.url3.com">Url 3</option>
</select>
<button id="redirect">Go</button>
$("#redirect").click(function() {
window.location.replace($(".mySelect").val());
});
You have to put http:// also
window.location.replace("http://"+$(this).val());
One option can be that you change the first option like
<select class="mySelect">
<option value="select">Select URL</option> <!--Default displayed--->
<option value="www.url1.com">Url 1</option>
<option value="www.url2.com">Url 2</option>
<option value="www.url3.com">Url 3</option>
</select>
or you can use click jquery event for combo box .
<form>
<select class="mySelect">
<option value="" selected="selected">Please Select</option>
<option value="www.url1.com">Url 1</option>
<option value="www.url2.com">Url 2</option>
<option value="www.url3.com">Url 3</option>
</select>
</form>
$(".mySelect").change( function() {
var n = $(this).val();
if(n != "") {
window.location.assign("http://"+n);
}
});
i have:
<select id="number" name="number">
<option value=""></option>
<option value="1">01</option>
<option value="2" selected="selected">02</option>
<option value="3">03</option>
<option value="4" selected="selected">04</option>
<option value="5">05</option>
</select>
and next with jQuery i use for example:
$('#number').val(1);
but this add next value - 1 and now i have:
<select id="number" name="number">
<option value=""></option>
<option value="1" selected="selected">01</option>
<option value="2" selected="selected">02</option>
<option value="3">03</option>
<option value="4" selected="selected">04</option>
<option value="5">05</option>
</select>
what i must use with:
$('#number').val(1);
that all others value can be not selected? So i would like - if i set new val then all others values can be reset. Maybe use each for reset, but how?
I know - this should be multiselect = false, but this is not my code - i must use this this jQuery.
Are you using a multiple select? Because the HTML you've added indicates that you're not. If you're not working with a multiple select, then your HTML with several selected="selected" doesn't make any sense.
If you are working with a multiple select, then setting .val() should clear the other selections. (Demo). If in some weird browser it isn't, you could try manually resetting the select:
$('#number option').prop('selected', false).val(1);
If clearing the current selection is not what you want, then you should consider setting the selected property manually, rather than using .val()
$('#number option[value=1]').prop('selected', true);
Demo