After Ajax Request Select Option not Resetting









up vote
0
down vote

favorite












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.










share|improve this question





















  • 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















up vote
0
down vote

favorite












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.










share|improve this question





















  • 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













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 23:08









Akshay Shrivastav

411421




411421











  • 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

















  • 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
















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













1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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>








share|improve this answer




















  • 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










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',
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
);



);













 

draft saved


draft discarded


















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








up vote
1
down vote



accepted










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>








share|improve this answer




















  • 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














up vote
1
down vote



accepted










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>








share|improve this answer




















  • 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












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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>








share|improve this answer












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>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 23:50









D. Smania

2,6441321




2,6441321











  • 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




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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto