I have a select drop down menu. The menu is just for show and doesn't contribute anything to the form. However, I want option "b" to display an alert/message when it is selected/clicked BUT then I want option "a" selected after the user closes the alert (similar to disabled).
<select name="fake">
<option name="a">a</option>
<option name="b">b</option>
</select>
I found other javascript code, but nothing that suits my particular need.
EDIT
This is half way to what I need:
<select onchange="selectChangeHandler(this)">
<option>a</option>
<option>b</option>
</select>
<script>
function selectChangeHandler(selectNode) {
if (selectNode.selectedIndex !== 2) {
alert("I'm alert option!");
}
}
</script>
However, when the user selects option "b" and the alert is displayed, I want the option to revert back to option "a"
you can call a javascript function on call onchange() event.
HTML
<select name="fake" onchange="change(this);">
<option value="a" name="a">a</option>
<option value="b" name="b">b</option>
</select>
javascript,
<script>
function change(selBox) {
for(var i, j = 0; i = selBox.options[j]; j++) {
if(i.value == "b" && selBox.selectedIndex != 0) {
alert("you've clicked b");
selBox.selectedIndex = 0;
}
}
}
</script>
$('#chk').change(function(){
if($(this).val() === 'a')
alert("You Clicked a!");
});
where chk is the id of select element
if you are using name then
$('#chk').change(function(){
if($(this).attr('name') === 'a')
alert("You Clicked a!");
});
where chk is the id of select element
Related
I have a simple selection dropdown that looks like this:
Criteria: <select name="criteriaon" id="criteriaon" onchange ="myfunction(this.form)">
<option value="1">ON</option>
<option value="0">OFF</option>
</select>
and a javascript that will pop up a confirmation box only if the user chooses OFF.
<script>
function myfunction(){
if (this.value === '0'){
confirm("Are you sure you want to turn off?");
}
}
document.getElementById('criteriaon').addEventListener('change', myfunction);
</script>
Right now what happens is that the selection will remain as OFF regardless of the user chose Yes or No. Is there a way to reset the selection to ON if the user chooses "No" on the pop-up confirmation box? Thanks in advance.
this way
ps: remove
--> onchange ="myfunction(this.form)"
because you are already using :
document.getElementById('criteriaon').addEventListener('change', myfunction);
document.getElementById('criteriaon').addEventListener('change', myfunction);
function myfunction() {
if (this.value === '0') {
if (!confirm("Are you sure you want to turn off?") ) {
this.value = '1'
}
}
}
Criteria:
<select name="criteriaon" id="criteriaon" >
<option value="1">ON</option>
<option value="0">OFF</option>
</select>
You can simply set the value of select to 1 by doing this.
this.value = '1';
function myfunction(){
if (this.value === '0'){
const result = confirm("Are you sure you want to turn off?");
if (!result) {
this.value = '1';
}
}
}
document.getElementById('criteriaon').addEventListener('change', myfunction);
Criteria: <select name="criteriaon" id="criteriaon" onchange="myfunction(this.form)">
<option value="1">ON</option>
<option value="0">OFF</option>
</select>
I have a select picker element like this in my web page :
<select id="difficulty" name="difficulty" class="selectpicker" multiple onchange="ChangeFilters()">
<option value="-1" selected >All</option>
<option value="0" >Facile</option>
<option value="1" >Normal</option>
<option value="2" >Difficile</option>
</select>
I want that when the user select the "All" options (value = '-1') , if there are other options selected they become not selected, and if the option all is selected when the user select an other options the "all option" become not selected.
I have searched for like 2 hours and i can't make this working.
I work with bootstrap and this select element is a selectpicker element, my javascript don't work when i try something ..
Please help !
EDIT :
I posted the working code as an answer, thanks for your help !
Something like this?
function ChangeFilters(){
var selVal = $('#difficulty').val()[0];
if(selVal == -1){
$('#difficulty').attr('multiple',false);
}else{
$('#difficulty').attr('multiple',true);
}
}
With the help of this post Select all / Unselect all in Bootstrap Select plugin
This is the full working code which allow the user to select the All option, 3 difficulties, or nothing :
HTML :
<select id="difficulty" name="difficulty" class="selectpicker" multiple onchange="ChangeFilters()">
<option value="All" selected >Tous</option>
<option value="0" >Facile</option>
<option value="1" >Normal</option>
<option value="2" >Difficile</option>
</select>
Javascript :
//This need to be on your page load or other load event before the first onChange.
$('#difficulty').data('allOptionIsSelected', true);
function ChangeFilters() {
if ($('#difficulty') != null) {
if ($('#difficulty').val() != null) {
var allOptionIsSelected = ($('#difficulty').val() || []).indexOf("All") > -1;
function valuesOf(elements) {
return $.map(elements, function (element) {
return element.value;
});
}
if ($('#difficulty').data('allOptionIsSelected') != allOptionIsSelected) {
//User clicked 'All' option
if (allOptionIsSelected) {
$('#difficulty').selectpicker('val', ["All"]);
$('#difficulty').selectpicker('refresh');
} else {
}
} else {
// User clicked other option
if ($('#difficulty').val().length != $('#difficulty').find('option').length) {
//user clicked other
// All options were selected, user deselected one option
// => unselect 'All' option
$('#difficulty').selectpicker('val', valuesOf($('#difficulty').find('option:selected[value!=All]')));
allOptionIsSelected = false;
}
}
$('#difficulty').data('allOptionIsSelected', allOptionIsSelected);
}
$('#difficulty').selectpicker('refresh');
}
}
If you want to make this work with your project, change '#difficulty' to the id of your select control.
Thanks to #yuriy636 for the previous help :)
I have HTML:
<select class="form" id="select" name="select">
<option>number1</option>
<option>number2</option>
</select>
<select name="organisation" class="form" id="cent" required>
<option value="1">UK</option>
<option value="2">SPAIN</option>
</select>
And another <select>:
<select class="form" id="ssip" name="organisation">
<option value="47" >US</option>
<option value="48">UKRAINE</option>
</select>
JQuery to show on 1 - first select on 2 - second
$(document).ready(function() {
$("#select").each(function () {
if (this.value == "1") {
$('#cent').show();
$('#ssip').hide();
}
if (this.value == "2") {
$('#cent').hide();
$('#ssip').show();
}
});
});
The problem is that when i choose option number1 appears first select with values 1 and 2 but when i select UK with value 1 and click submit it's sending value 47 , of second select's first option..
Try disabling the opposite select when toggling the selects. A control on a form will still be submitted if its display is set to none, however if the control is disabled it will not be submitted with the form.
$(document).ready(function() {
$("select").change(function () {
if (this.value == "number1") {
$('#cent').show().prop("disabled", false);
$('#ssip').hide().prop("disabled", true);
}
if (this.value == "number2") {
$('#cent').hide().prop("disabled", true);
$('#ssip').show().prop("disabled", false);
}
});
});
JS Fiddle: http://jsfiddle.net/bTJsH/
You are trying to loop one select box..
$("#select").each(function () {
There can be only one element with id "select". You should check like:
if ($("#select").value() == "1") { ..
And you don't even have values in your first select options..
i might suggest something like this
$('#select').on('change', function() {
var selected = this.selectedIndex;
$('.orgs').hide();
switch(selected) {
case 1:
$('#ssip').show();
break;
case 2:
$('#cent').show();
break;
}
});
here is an working example http://jsfiddle.net/pixelchemist/7ZGwy/
I want to hide a table row based on the value selected from a radio button.
Those radio buttons were created based on value selected from a dropdown list box.
Here is my code
HTML code for listbox
<select name="txtLT" id="LT" >
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
Jquery
$(document).ready(function() {
$("#LT").change(function() {
var selVal = $(this).val();
$("#tblerow").html('');
if(selVal == 'c') {
$("#tblerow").append('<td><input type="radio" id="cmpof" name="cmpof" value="1" onclick="show()" />1 <input type="radio" id="cmpof" name="cmpof" value="2" onclick="hide()"/>2</td>');
}
});
});
Now if radio button 2 is selected, then the table row with tr2 should disappear.
Please help me to find the solution.
EDIT
I tried the following JavaScript, but it does not hide the row.
function show() { document.getElementById('sbjrow').style.display = 'block'; }
function hide() { document.getElementById('sbjrow').style.display = 'none'; }
Could you please suggest what is wrong here?
You can just use the hide and show function of jQuery like so:
$(function () {
$('#LT').change(function () {
if ($(this).val() == 'c') {
$('.radios').show();
} else {
$('.radios').hide();
}
});
$('input[name=cmpof]').change(function () {
if ($('#LT').val() == 'c' && $(this).val() == 'bar') {
$('#table tr').eq(1).hide();
} else {
$('#table tr').eq(1).show();
}
});
});
If you don't want the table row to show up by default, just do it with css.
JsFiddle example: http://jsfiddle.net/AX5TJ/1/
Updated version that may be closer to what you want: http://jsfiddle.net/AX5TJ/2/
Also be carefull, you have a syntax error in your script : when you define $("#LT").change(function() {, you have to close with });
I need to be able to hide an image that appears when clicking on an option within a select field ONLY if the value="" (nothing inside quotes). If the value="some_url" inside the option, then I want the image to show.
I have used the following code to SHOW the image when an option is clicked. But when using onClick, it shows the image even if the option value="".
Here is the Javascript I'm using:
function showImage() {
document.getElementById('openimg').style.display = 'block';
Here is the html:
<select name="" >
<option value="url" onclick="showImage();">Some_option_1</option>
<option value="">Some_option_2</option>
<option value="">Some_option_3</option>
</select>
<a href='url_2'><img src='images/some_img.jpg' id='openimg' style='display:none'></a>
I only inserted one onClick command inside one option, just to show that it works. It seems I need an if statement to "show if" or "hide if" along with the onClick command within each option.
this is how I would do it:
<script type="text/javascript">
function showImage()
{
var choice = document.getElementById('myDropDown').value;
if(choice.length > 0)
{
document.getElementById('openimg').style.display = 'block';
}
else
{
document.getElementById('openimg').style.display = 'none';
}
}
</script>
<select id="myDropDown" onchange="showImage()">
<option value="url">Some_option_1</option>
<option value="">Some_option_2</option>
<option value="">Some_option_3</option>
</select>
<a href='url_2'><img src='images/some_img.jpg' id='openimg' style='display:none'></a>
Um, are you asking how to use an if or how to determine what is selected?
First of all, use onchange event in the dropdown.
This is the LONGHAND way of doing it for illustration purposes.
function onChange(){
var mySelect = document.getElementById("my-select");
var selectedValue = "";
for( var i = 0; i < mySelect.length;i++){
if( mySelect[i].selected)
selectedValue = mySelect[i].value;
}
if( selectedValue == "whatever")
{
//do something
}
if( selectedValue == "ugh")
// do something else
}