After Ajax Request Select Option not Resetting
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
add a comment |
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
try$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid ofdisabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16
add a comment |
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
jquery
asked Nov 10 at 23:08
Akshay Shrivastav
461421
461421
try$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid ofdisabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16
add a comment |
try$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid ofdisabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16
try
$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
try
$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
1
Can't select a disabled option. Get rid of
disabled
. Can simplify also by just setting value of select ... $('.post_category').val('')
– charlietfl
Nov 10 at 23:16
Can't select a disabled option. Get rid of
disabled
. Can simplify also by just setting value of select ... $('.post_category').val('')
– charlietfl
Nov 10 at 23:16
add a comment |
1 Answer
1
active
oldest
votes
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53244311%2fafter-ajax-request-select-option-not-resetting%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
{
$(".select2").select2();
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
{
$(".select2").select2({
placeholder: "Select Post Category",
allowClear: true
});
setTimeout(
function(){$("#id_h5_single").val("Am").change();},
1000
);
setTimeout(
function(){$("#id_h5_single").val("").change();},
5000
);
});
.select2{
width: 50vw;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
answered Nov 10 at 23:50
Shidersz
3,3361425
3,3361425
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted
$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted
$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53244311%2fafter-ajax-request-select-option-not-resetting%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
try
$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid of
disabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16