Greeting fellow code-jugglers!
I am dealing with a kinda frustrating problem, which it seems I can't solve to 100% I want it to work.
I've got a select field with various dynamic options amount (could be more than one "middle", e. g. "middle 1", "middle 2") deactivated and fetched and build by php. The user has to input a number to activate and pre-select the specific select option. So far it works to the point, it pre-selects the option based on the user input. But I want it to enable every select option above the pre-selection.
$(document).ready(function() {
// PRE-SELECT TPOS BASED ON TMEMBER
$('#inputX').change(function() {
var member_id = $('#inputX').val();
// FETCHED BY PHP, NO NEED TO OVERRIDE (WORKS)
var event_id = <? echo $event_id; ?>;
var posit_id = <? echo $posit_id; ?>;
// FETCH CURRENT POSITION, MEMBER IS AT
$.ajax({
type: 'POST',
url: 'fetch_pos.php',
data: {
posit_id: posit_id,
event_id: event_id,
member_id: member_id
},
// GET SELECT OPTION WITH POSITION VALUE MEMBER IS AT [E. G. "START"]
success: function(html){
// ENABLE SELECT OPTION
$("#selectX option[value='" + html + "']").attr("disabled", false);
// ADD SELECTED ATTRIBUTE
$("#selectX").val(html).setAttribute("selected", "selected");
// DISABLE EVERY OPTION PAST THE RETURNED VALUE AND
// ENABLE EVERY "PREVIOUS" OPTION
$('select#selectX').val(html).children().attr("disabled", false).filter(':selected').nextAll().prop('disabled', true);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" name="inputX" id="inputX" required="required" />
<select name="selectX" id="selectX" required="required">
<option selected="selected">Awaiting Input</option>
<option disabled="disabled" value="Start">Start</option>
<option disabled="disabled" value="Middle">Middle</option>
<option disabled="disabled" value="Last">Last</option>
</select>
</form>
The returned value of AJAX is one of the option values, since my code needs them to be worked with and not integers as 1, 2 or 3(+).
For example:
I've got the same select field but the member with id number 3 is at position goal. So "goal", "middle", and "start" should be enabled. If member number 3 is at position middle, I want only "start" and "middle" to be enabled.
Any help?
Sincerely,
montreal
EDIT
Because of misunderstanding: ignore the AJAX. It just returns the value as position of the member, because the option value is defined as such --> E. G. Member with ID 1 is at position "middle", hence option value="middle" and everything before (option value="start") should be enabled.
I just want to enable every option based on the selectIndex from the returned value selected option minus total amount of options.
Example:
3 options
returned value is "middle", hence selectIndex 1
select option with selectIndex 0 and 1 should be enabled
I think you are looking for something like:
$('select#test')
//set new value on select
.val('4')
// enable all children
.children().prop('disabled', false)
// starting at selected option, disable all after it
.filter(':selected').nextAll().prop('disabled', true)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="test" size="10">
<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>
<option value="6">item 6</option>
<option value="7">item 7</option>
<option value="8">item 8</option>
<option value="9">item 9</option>
<option value="10">item 10</option>
</select>
Related
I have used a select box to identify the users age group. Accroding to the value of the age group, I need to display diffrent options
For eg.
<select name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
If the user selects the first option I need to display contents of 'tbl_hobbies' (mysql table) with age group 1 in another select box.
<select name="hobbies" id="hobbies">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
For the second option (26-35), I need to display 'tbl_hobbies' contents with age group 2
<select name="hobbies" id="hobbies">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
How can I achieve this using AJAX or is this possible without using AJAX?
First, add an id to age group
<select name="age_group" id="age_group">
Then
$( document ).ready(function() {
$('#age_group').change(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data: { group_id: $(this).val() },
dataType: "html"
})
.done(function( msg ) {
$('#hobbies').html(msg);
});
});
});
In ajax.php you get all hobbies where group_id = $_POST['group_id']
$sql = "SELECT name FROM tbl_hobbies WHERE group_id = ".$_POST['group_id'];
// Execute your query and put the result in a variable (example $result_query)
// Then loop it
foreach($result_query as $row) {
echo '<option>'.$row['name'].'</option>';
}
NB : The loop may change according to your method to retrieve data
You can simply put all option together and hide option as per user choice
<select name="hobbies" id="hobbies">
<option class="group_1">Cleaning Home</option>
<option class="group_1">Reading</option>
<option class="group_1">Travelling</option>
<option class="group_2">watching movies</option>
<option class="group_2">playing computer games</option>
<option class="group_2">chatting</option>
you can use like this
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option>watching movies</option>
<option>playing computer games</option>
<option>chatting</option>
</select>
<select name="hobbies2" id="hobbies2">
<option>Cleaning Home</option>
<option>Reading</option>
<option>Travelling</option>
</select>
and your jquery script look like this
<script>
$('#age_select').on('change'.function(){
var value = $(this).val();
if ( value == '1' ) {
$('#hobbies1').show();
$('#hobbies2').hide();
} else if ( value == '2' ) {
$('#hobbies2').show();
$('#hobbies1').hide();
}
});
</script>
and if you want to pass through ajax
$('#age_select').on('change'.function(){
var value = $(this).val();
var data = 'age='+value;
$.post(
'abc.php',
data
).success(function(resp){
});
});
What you want is called "Dynamic dependent dropdowns". You can view a very good demo here.
Algorithm
1. Get all the options from database // do that using php
2. Assign classes to them according to group // do that using php
3. Put all of them in HTML // do that using php
4. Show/hide on event // do that using jQuery/javascript
Example
<select id="age_select" name="age_group">
<option value="1">15-25</option>
<option value="2">26-35</option>
<option value="3">35-60</option>
<option value="4">60 or above</option>
</select>
<select name="hobbies1" id="hobbies1">
<option value="wm" class="grp age-grp-1">watching movies</option>
<option value="pcg" class="grp age-grp-1>playing computer games</option>
<option value="chat" class="grp age-grp-2>chatting</option>
<option value="ch" class="grp age-grp-2>Cleaning Home</option>
<option value="rea" class="grp age-grp-3>Reading</option>
<option value="tr" class="grp age-grp-4>Travelling</option>
</select>
<script type="text/javascript">
// script to execute when you change option from first dropdown
$('#age_select').on('change'.function(){
var ageGrp = $(this).val();
// first, hide all options
$('.grp').hide();
// show options only for selected group
$('.age-grp-'+ageGrp).show();
});
</script>
Hope this helps!!!
I used this code to hide a selected option from dropdownmenu 2. But when a user after that changes the first option, i want tot reset the second dropdown list to prevent that the same value is selected twice.
ex.
List 1 : select A
List 2 : select B
List 1 : select B **
** How can I reset list 2 at this moment when list 1 is changed? This to prevent that list 1 and list 2 get the same value (B)!
Thanks and kind regards
HMTL:
<select name="anfang">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<select name="secondSelect">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
JavaScript:
var select1 = document.querySelector('select[name="anfang"]'),
secondList= document.querySelectorAll('select[name="secondSelect"]
option');
select1.onchange = function(){
var selected = this.value;
for(var i=0;i<secondList.length;i++){
if(secondList[i].value==selected)
secondList[i].setAttribute('disabled',true);
else if(secondList[i].getAttribute('disabled'))
secondList[i].removeAttribute('disabled');
}
}
You can do this a few ways. I personally like to set it to a blank value, but you can just unselect the value you are disabling like:
if(secondList[i].value==selected) {
secondList[i].setAttribute('disabled',true);
secondList[i].selected = false;
} else if(secondList[i].getAttribute('disabled')) {
secondList[i].removeAttribute('disabled');
}
See example.
I am new to web development, need help with PHP coding for a form.
A form with 5 dropdown list needs to be created. The values in 1st dropdown list would be "A,B,C,D,E,F,G". If suppose "C" is selected in 1st dropdown, then 2nd dropdown will have all the values except "C" i.e; "A,B,D,E,F,G". If suppose we select "A" in second dropdown, the values shown in 3rd dropdown would be all the values except the value chosen in dropdown 1 & 2. So value in dropdown 3 would be "B,D,E,F,G.
Similarly, it will happen for other two dropdown boxes.
One way you could try is to keep track of what options need to be removed in an array. Then you could define which select controls which. Take this example:
given this html
<select id="a" var="b">
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
<select id="b" var="c" disabled>
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
<select id="c" disabled>
<option>-- SELECT --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
you could use this jquery
var doNotShow = [];
$('select').change( function() {
var nextSelect = $(this).attr("var");
doNotShow.push($(this).val());
$.each(doNotShow, function( index, value ) {
$("#" + nextSelect + " option[value='" + value + "']").prop('disabled', true);
});
$("#" + nextSelect).prop('disabled', false);
$(this).prop('disabled', true);
});
doNotShow.push($(this).val()); keeps track of all the previously selected values, then the $.each removes (or in this case, disables) the unwanted options from the next select in line, which is stored in the select's var property.
Here's an example.
This will be done with jquery or even with Javasript, with jQuery you can read the documentation of .change() function, and for Javascript with the onchange Attribute, try to read this question it will help you a little bit : HTML SELECT - Trigger JavaScript ONCHANGE event even when the option is not changed
I've built a dynamically populated multiselect form for my site. When the user selects one or more items in the first select, the second is filled via jQuery.
The form works correctly except for the order the multiselect is filled.
e.g. The "product" select contains:
<select name="product_id" id="products" multiple="multiple">
<option value="#" selected="selected">Please Select</option>
<option value="1">Product 1</option>
<option value="5">Product 2</option>
<option value="4">Product 3</option>
<option value="6">Product 4</option>
(Yes, the values are not the same as the product numbers)
When a user selects one of these the jQuery call populates the next dropdown.
<select name="version_id" id="versions" multiple="multiple">
<option value="100">3.0.7</option>
<option value="101">1.0.8</option>
<option value="102">12.0.1</option>
The code fills this select and orders by "value", is there a way to force it to order by the actual content e.g. change it to:
<option value="101">1.0.8</option>
<option value="100">3.0.7</option>
<option value="102">12.0.1</option>
The success code in my call is:
success: function(versions)
{
$.each(versions,function(id,version)
{
var opt = $('<option />');
opt.val(id);
opt.text(version);
$('#versions').append(opt);
});
}
Thanks!
Thanks to #deifwud for pointing me in the right direction. In the end I added the following directly after my success function. It re-orders the results sorting by the text instead of the value.
$("#versions").html($("#versions option").sort(function (a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}));
you must be doing a ajax call to a file where a sql query is executed to dynamically generate the next dropdown box.... if this is the case then you need to place the keyword "ASC" at the last of select query which is yielding your "id" in value attribute of <option value"">
Hmm...this question may sounds silly , hope you all don't mind...
If I have a drop list:
<select name="myoption" onchange="document.textbox.value=this.value">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<input type="text" name="textbox" id="textbox">
So with this now the textbox will display what is selected , but is it possible to display the A,B,C instead of 1,2,3?
Actually I need a drop-list which will display 2 different values to 2 textbox,such as if A is selected,textbox1 will display "A" and textbox2 will display "1".
I don't know if it is possible and I tried for some times already...can someone give me some hints?
Thanks in advance.
Yes, it is possible. I would just externalize all javascript into a separate file to avoid mixing markup and scripts.
So the script:
// subscribe for the DOM ready event to ensure that you
// are manipulating the DOM only when it is loaded
window.onload = function() {
// subscribe for the onchange event of the dropdown
document.getElementById('myoption').onchange = function() {
// fetch the text of the currently selected element
var text = this.options[this.selectedIndex].innerHTML;
// and assign it to the corresponding input
document.getElementById('textbox').value = text;
};
};
and the markup:
<select name="myoption" id="myoption">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<input type="text" name="textbox" id="textbox" />
and a live demo.