Select box value not changing
up vote
-1
down vote
favorite
I have a html select box which I am appending new options to depending on the selection from a previous select box. Before I append the new options I need to empty the select box so only the options required appear.
The append works fine and the options change and can be selected from the '2nd_incident' select box, however as soon as I incorporate .empty() the options still change as I want but when you actually try to select them from the select box the value in the select does not change.
Logging the value to the console confirms that the value has changed but it just doesn't change visually.
I have also tried .html() in place of the .empty() and .append() but this also has the same issue.
$("#load_section3").change(function() {
var first_incident = document.getElementById('1st_incident').value;
var second_incident = document.getElementById('2nd_incident');
console.log(second_incident.value);
if (first_incident == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (first_incident == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
Any help will be appreciated as I feel as though I have been googling this for weeks now.
javascript jquery html
add a comment |
up vote
-1
down vote
favorite
I have a html select box which I am appending new options to depending on the selection from a previous select box. Before I append the new options I need to empty the select box so only the options required appear.
The append works fine and the options change and can be selected from the '2nd_incident' select box, however as soon as I incorporate .empty() the options still change as I want but when you actually try to select them from the select box the value in the select does not change.
Logging the value to the console confirms that the value has changed but it just doesn't change visually.
I have also tried .html() in place of the .empty() and .append() but this also has the same issue.
$("#load_section3").change(function() {
var first_incident = document.getElementById('1st_incident').value;
var second_incident = document.getElementById('2nd_incident');
console.log(second_incident.value);
if (first_incident == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (first_incident == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
Any help will be appreciated as I feel as though I have been googling this for weeks now.
javascript jquery html
1
The problem is that as soon as you select something from the 2nd dropdown, the change event gets triggered and the 2nd dropdown is emptied and rewritten. So while the choice is logged correctly, this resets the selected index to 0. A quick way to fix this is to replace$("#load_section3").change
with$("#1st_incident").change
– Chris G
Nov 7 at 9:32
This worked, thank you !!
– Chunky Lova
Nov 7 at 9:35
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a html select box which I am appending new options to depending on the selection from a previous select box. Before I append the new options I need to empty the select box so only the options required appear.
The append works fine and the options change and can be selected from the '2nd_incident' select box, however as soon as I incorporate .empty() the options still change as I want but when you actually try to select them from the select box the value in the select does not change.
Logging the value to the console confirms that the value has changed but it just doesn't change visually.
I have also tried .html() in place of the .empty() and .append() but this also has the same issue.
$("#load_section3").change(function() {
var first_incident = document.getElementById('1st_incident').value;
var second_incident = document.getElementById('2nd_incident');
console.log(second_incident.value);
if (first_incident == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (first_incident == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
Any help will be appreciated as I feel as though I have been googling this for weeks now.
javascript jquery html
I have a html select box which I am appending new options to depending on the selection from a previous select box. Before I append the new options I need to empty the select box so only the options required appear.
The append works fine and the options change and can be selected from the '2nd_incident' select box, however as soon as I incorporate .empty() the options still change as I want but when you actually try to select them from the select box the value in the select does not change.
Logging the value to the console confirms that the value has changed but it just doesn't change visually.
I have also tried .html() in place of the .empty() and .append() but this also has the same issue.
$("#load_section3").change(function() {
var first_incident = document.getElementById('1st_incident').value;
var second_incident = document.getElementById('2nd_incident');
console.log(second_incident.value);
if (first_incident == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (first_incident == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
Any help will be appreciated as I feel as though I have been googling this for weeks now.
$("#load_section3").change(function() {
var first_incident = document.getElementById('1st_incident').value;
var second_incident = document.getElementById('2nd_incident');
console.log(second_incident.value);
if (first_incident == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (first_incident == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
$("#load_section3").change(function() {
var first_incident = document.getElementById('1st_incident').value;
var second_incident = document.getElementById('2nd_incident');
console.log(second_incident.value);
if (first_incident == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (first_incident == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
javascript jquery html
javascript jquery html
edited Nov 7 at 9:28
Chris G
5,8392821
5,8392821
asked Nov 7 at 9:24
Chunky Lova
11
11
1
The problem is that as soon as you select something from the 2nd dropdown, the change event gets triggered and the 2nd dropdown is emptied and rewritten. So while the choice is logged correctly, this resets the selected index to 0. A quick way to fix this is to replace$("#load_section3").change
with$("#1st_incident").change
– Chris G
Nov 7 at 9:32
This worked, thank you !!
– Chunky Lova
Nov 7 at 9:35
add a comment |
1
The problem is that as soon as you select something from the 2nd dropdown, the change event gets triggered and the 2nd dropdown is emptied and rewritten. So while the choice is logged correctly, this resets the selected index to 0. A quick way to fix this is to replace$("#load_section3").change
with$("#1st_incident").change
– Chris G
Nov 7 at 9:32
This worked, thank you !!
– Chunky Lova
Nov 7 at 9:35
1
1
The problem is that as soon as you select something from the 2nd dropdown, the change event gets triggered and the 2nd dropdown is emptied and rewritten. So while the choice is logged correctly, this resets the selected index to 0. A quick way to fix this is to replace
$("#load_section3").change
with $("#1st_incident").change
– Chris G
Nov 7 at 9:32
The problem is that as soon as you select something from the 2nd dropdown, the change event gets triggered and the 2nd dropdown is emptied and rewritten. So while the choice is logged correctly, this resets the selected index to 0. A quick way to fix this is to replace
$("#load_section3").change
with $("#1st_incident").change
– Chris G
Nov 7 at 9:32
This worked, thank you !!
– Chunky Lova
Nov 7 at 9:35
This worked, thank you !!
– Chunky Lova
Nov 7 at 9:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
After going through your code i found that everytime you select values in selectbox it is checking for if conditions and updating the select boxes according to them. so here could be possible solution for your problem. You should only populate value to second selectbox on change event of first selectbox
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
After going through your code i found that everytime you select values in selectbox it is checking for if conditions and updating the select boxes according to them. so here could be possible solution for your problem. You should only populate value to second selectbox on change event of first selectbox
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
add a comment |
up vote
0
down vote
After going through your code i found that everytime you select values in selectbox it is checking for if conditions and updating the select boxes according to them. so here could be possible solution for your problem. You should only populate value to second selectbox on change event of first selectbox
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
add a comment |
up vote
0
down vote
up vote
0
down vote
After going through your code i found that everytime you select values in selectbox it is checking for if conditions and updating the select boxes according to them. so here could be possible solution for your problem. You should only populate value to second selectbox on change event of first selectbox
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
After going through your code i found that everytime you select values in selectbox it is checking for if conditions and updating the select boxes according to them. so here could be possible solution for your problem. You should only populate value to second selectbox on change event of first selectbox
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="load_section3">
<select class="inputs" id="1st_incident">
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<select class="inputs" id="2nd_incident">
<option value="Test">NA</option>
</select>
</div>
<script>
$("#1st_incident").change(function(){
if (this.value == 1) {
//remove na and input options into next select box
$('#2nd_incident').empty().end();
$('#2nd_incident').append('<option > Please Select </option>' +
'<option > something1 </option>' +
'<option > something2 </option>' +
'<option > something3 </option>' +
'<option > something3 </option>');
} else if (this.value == 2) {
$('#2nd_incident').empty().append('<option> Please Select </option>' +
'<option> somethings1 </option>' +
'<option> somethings2 </option>' +
'<option> somethings3 </option>');
} else {
$('#2nd_incident').empty();
$('#2nd_incident').append('<option> NA </option>');
}
});
</script>
<body>
<html>
answered Nov 7 at 10:04
Deepak Verma
37411
37411
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186582%2fselect-box-value-not-changing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The problem is that as soon as you select something from the 2nd dropdown, the change event gets triggered and the 2nd dropdown is emptied and rewritten. So while the choice is logged correctly, this resets the selected index to 0. A quick way to fix this is to replace
$("#load_section3").change
with$("#1st_incident").change
– Chris G
Nov 7 at 9:32
This worked, thank you !!
– Chunky Lova
Nov 7 at 9:35