javascript: refresh same page with query string from selects












0















I have 2 selects: month and year. When the user selects the year and the month then I want to redirect to the same page with a query string produced by the selections.



So this is what I have done:



<div id="custom-select1" class="custom-select" style="width:200px;">
<select name="month" onchange="if (this.value) window.location.href=this.value">
<option value="0">Select Month:</option>
<option value="?date_from={value-from-year-select}-01-01&date_to={value-from-year-select}-01-31&courses=on&events=on">January</option>
<option value="..">February</option>
<option value="..">March</option>
<option value="..">April</option>
<option value="..">May</option>
<option value="..">June</option>
<option value="..">July</option>
<option value="..">August</option>
<option value="..">September</option>
<option value="..">Octomber</option>
<option value="..">November</option>
<option value="..">December</option>
</select>
</div>
<div class="custom-select" style="width:200px;">
<select name="year" >
<option value="2000">Select Year:</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
</div>


How I can replace {value-from-year-select} to get the value of the year selected?










share|improve this question























  • Is the goal to not catch the event with javascript and then do the redirect? What if the user choses month and wants to change year after? Why not make it a simple form with a submit button and method GET if you don't want to catch the event and read the data?

    – Patrick
    Nov 26 '18 at 12:13











  • @Patrick because the big idea is to create a calendar which will be filtered with the selected values of these selects. So I don't want to add a button, but if it's not possible then I will use one

    – dvn22
    Nov 26 '18 at 12:16











  • So what you want to do is catch the change of the select boxes, see if the user has selected any values and then redirect the user depending on values chosen. If you don't want to build javascript separately you would have to check the value of "year" in the onchange attribute.

    – Patrick
    Nov 26 '18 at 12:23











  • @Patrick ok I think I got it... So I will have to use 2 onchange (one for each select) and check if there is a value selected in the other and pass both values to the query string. right?

    – dvn22
    Nov 26 '18 at 12:27
















0















I have 2 selects: month and year. When the user selects the year and the month then I want to redirect to the same page with a query string produced by the selections.



So this is what I have done:



<div id="custom-select1" class="custom-select" style="width:200px;">
<select name="month" onchange="if (this.value) window.location.href=this.value">
<option value="0">Select Month:</option>
<option value="?date_from={value-from-year-select}-01-01&date_to={value-from-year-select}-01-31&courses=on&events=on">January</option>
<option value="..">February</option>
<option value="..">March</option>
<option value="..">April</option>
<option value="..">May</option>
<option value="..">June</option>
<option value="..">July</option>
<option value="..">August</option>
<option value="..">September</option>
<option value="..">Octomber</option>
<option value="..">November</option>
<option value="..">December</option>
</select>
</div>
<div class="custom-select" style="width:200px;">
<select name="year" >
<option value="2000">Select Year:</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
</div>


How I can replace {value-from-year-select} to get the value of the year selected?










share|improve this question























  • Is the goal to not catch the event with javascript and then do the redirect? What if the user choses month and wants to change year after? Why not make it a simple form with a submit button and method GET if you don't want to catch the event and read the data?

    – Patrick
    Nov 26 '18 at 12:13











  • @Patrick because the big idea is to create a calendar which will be filtered with the selected values of these selects. So I don't want to add a button, but if it's not possible then I will use one

    – dvn22
    Nov 26 '18 at 12:16











  • So what you want to do is catch the change of the select boxes, see if the user has selected any values and then redirect the user depending on values chosen. If you don't want to build javascript separately you would have to check the value of "year" in the onchange attribute.

    – Patrick
    Nov 26 '18 at 12:23











  • @Patrick ok I think I got it... So I will have to use 2 onchange (one for each select) and check if there is a value selected in the other and pass both values to the query string. right?

    – dvn22
    Nov 26 '18 at 12:27














0












0








0








I have 2 selects: month and year. When the user selects the year and the month then I want to redirect to the same page with a query string produced by the selections.



So this is what I have done:



<div id="custom-select1" class="custom-select" style="width:200px;">
<select name="month" onchange="if (this.value) window.location.href=this.value">
<option value="0">Select Month:</option>
<option value="?date_from={value-from-year-select}-01-01&date_to={value-from-year-select}-01-31&courses=on&events=on">January</option>
<option value="..">February</option>
<option value="..">March</option>
<option value="..">April</option>
<option value="..">May</option>
<option value="..">June</option>
<option value="..">July</option>
<option value="..">August</option>
<option value="..">September</option>
<option value="..">Octomber</option>
<option value="..">November</option>
<option value="..">December</option>
</select>
</div>
<div class="custom-select" style="width:200px;">
<select name="year" >
<option value="2000">Select Year:</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
</div>


How I can replace {value-from-year-select} to get the value of the year selected?










share|improve this question














I have 2 selects: month and year. When the user selects the year and the month then I want to redirect to the same page with a query string produced by the selections.



So this is what I have done:



<div id="custom-select1" class="custom-select" style="width:200px;">
<select name="month" onchange="if (this.value) window.location.href=this.value">
<option value="0">Select Month:</option>
<option value="?date_from={value-from-year-select}-01-01&date_to={value-from-year-select}-01-31&courses=on&events=on">January</option>
<option value="..">February</option>
<option value="..">March</option>
<option value="..">April</option>
<option value="..">May</option>
<option value="..">June</option>
<option value="..">July</option>
<option value="..">August</option>
<option value="..">September</option>
<option value="..">Octomber</option>
<option value="..">November</option>
<option value="..">December</option>
</select>
</div>
<div class="custom-select" style="width:200px;">
<select name="year" >
<option value="2000">Select Year:</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
</div>


How I can replace {value-from-year-select} to get the value of the year selected?







javascript html select






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 12:04









dvn22dvn22

937




937













  • Is the goal to not catch the event with javascript and then do the redirect? What if the user choses month and wants to change year after? Why not make it a simple form with a submit button and method GET if you don't want to catch the event and read the data?

    – Patrick
    Nov 26 '18 at 12:13











  • @Patrick because the big idea is to create a calendar which will be filtered with the selected values of these selects. So I don't want to add a button, but if it's not possible then I will use one

    – dvn22
    Nov 26 '18 at 12:16











  • So what you want to do is catch the change of the select boxes, see if the user has selected any values and then redirect the user depending on values chosen. If you don't want to build javascript separately you would have to check the value of "year" in the onchange attribute.

    – Patrick
    Nov 26 '18 at 12:23











  • @Patrick ok I think I got it... So I will have to use 2 onchange (one for each select) and check if there is a value selected in the other and pass both values to the query string. right?

    – dvn22
    Nov 26 '18 at 12:27



















  • Is the goal to not catch the event with javascript and then do the redirect? What if the user choses month and wants to change year after? Why not make it a simple form with a submit button and method GET if you don't want to catch the event and read the data?

    – Patrick
    Nov 26 '18 at 12:13











  • @Patrick because the big idea is to create a calendar which will be filtered with the selected values of these selects. So I don't want to add a button, but if it's not possible then I will use one

    – dvn22
    Nov 26 '18 at 12:16











  • So what you want to do is catch the change of the select boxes, see if the user has selected any values and then redirect the user depending on values chosen. If you don't want to build javascript separately you would have to check the value of "year" in the onchange attribute.

    – Patrick
    Nov 26 '18 at 12:23











  • @Patrick ok I think I got it... So I will have to use 2 onchange (one for each select) and check if there is a value selected in the other and pass both values to the query string. right?

    – dvn22
    Nov 26 '18 at 12:27

















Is the goal to not catch the event with javascript and then do the redirect? What if the user choses month and wants to change year after? Why not make it a simple form with a submit button and method GET if you don't want to catch the event and read the data?

– Patrick
Nov 26 '18 at 12:13





Is the goal to not catch the event with javascript and then do the redirect? What if the user choses month and wants to change year after? Why not make it a simple form with a submit button and method GET if you don't want to catch the event and read the data?

– Patrick
Nov 26 '18 at 12:13













@Patrick because the big idea is to create a calendar which will be filtered with the selected values of these selects. So I don't want to add a button, but if it's not possible then I will use one

– dvn22
Nov 26 '18 at 12:16





@Patrick because the big idea is to create a calendar which will be filtered with the selected values of these selects. So I don't want to add a button, but if it's not possible then I will use one

– dvn22
Nov 26 '18 at 12:16













So what you want to do is catch the change of the select boxes, see if the user has selected any values and then redirect the user depending on values chosen. If you don't want to build javascript separately you would have to check the value of "year" in the onchange attribute.

– Patrick
Nov 26 '18 at 12:23





So what you want to do is catch the change of the select boxes, see if the user has selected any values and then redirect the user depending on values chosen. If you don't want to build javascript separately you would have to check the value of "year" in the onchange attribute.

– Patrick
Nov 26 '18 at 12:23













@Patrick ok I think I got it... So I will have to use 2 onchange (one for each select) and check if there is a value selected in the other and pass both values to the query string. right?

– dvn22
Nov 26 '18 at 12:27





@Patrick ok I think I got it... So I will have to use 2 onchange (one for each select) and check if there is a value selected in the other and pass both values to the query string. right?

– dvn22
Nov 26 '18 at 12:27












3 Answers
3






active

oldest

votes


















3














I would have chosen to use jquery to catch the change event (it's just what I'm used to). But my guess is that you want to keep your vanilla JS approach, you could write an eventlistener in javascript instead of using the onchange attribute. But here is a solution that keeps to how you work right now. Just add the javascript in a script tag in the DOM.



function customSelectChange() {
var year = document.getElementsByName("year");
var month = document.getElementsByName("month");
if(year[0].value !== "0" && month[0].value !== "0"){
// DO your redirect
alert(year[0].value+'-'+month[0].value);
}
}


https://jsfiddle.net/4d9phtgv/






share|improve this answer































    3














    With delay






    const month = document.getElementById('month');
    const year = document.getElementById('year');

    function handleChange() {
    if (month.value && year.value) {
    const urlSearch = new URLSearchParams();
    urlSearch.append('month', month.value);
    urlSearch.append('year', year.value);
    console.log(urlSearch.toString());
    }
    }

    <select onchange="handleChange()" name="month" id="month">
    <option value="">Choice month</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    </select>
    <select onchange="handleChange()" name="year" id="year">
    <option value="">Choice year</option>
    <option value="2001">2001</option>
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2004</option>
    <option value="2005">2005</option>
    <option value="2006">2006</option>
    <option value="2007">2007</option>
    <option value="2008">2008</option>
    <option value="2009">2009</option>
    <option value="2010">2010</option>
    <option value="2011">2011</option>
    <option value="2012">2012</option>
    </select>








    share|improve this answer































      0














      You can use a form and submit it to refresh the page with the info, to add the extra fields you can use hidden inputs.






      monthEnd = function(year, month) {

      date = new Date(year, month - 1, 1);

      var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
      return lastDay.getDate();
      }

      appendHidden = function(formId, name, value) {
      $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
      }

      begining = function() {
      year = $('#selectYear').val();
      month = $('#selectMonth').val();

      return year + '-' + month + '-' + '1'
      }

      end = function() {
      year = $('#selectYear').val();
      month = $('#selectMonth').val();

      return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
      }

      $( document ).ready(function() {
      months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

      for(i=0; i< months.length; i++) {
      $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
      }


      for(y=2000; y<= 2023;y++) {
      $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
      }


      $('#selectMonth').on('change', function() {
      appendHidden('form', 'courses', 'on');
      appendHidden('form', 'events', 'on');
      appendHidden('form', 'date-from', begining());
      appendHidden('form', 'date-from', end());
      $('#selectYear').remove();
      $('#selectMonth').remove();
      //$('#form').submit(); Uncomment this line to submit and refresh
      alert($('#form').serialize())
      });



      });

      .hidden {
      display: none;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <form id="form" action="" method="GET">
      <div id="custom-select1" class="custom-select" style="width:200px;" >
      <select id="selectMonth" name="month" >
      </select>
      </div>
      <div class="custom-select" style="width:200px;">
      <select id="selectYear" name="year" ></select>
      </div>
      </form>








      share|improve this answer


























        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
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480727%2fjavascript-refresh-same-page-with-query-string-from-selects%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        I would have chosen to use jquery to catch the change event (it's just what I'm used to). But my guess is that you want to keep your vanilla JS approach, you could write an eventlistener in javascript instead of using the onchange attribute. But here is a solution that keeps to how you work right now. Just add the javascript in a script tag in the DOM.



        function customSelectChange() {
        var year = document.getElementsByName("year");
        var month = document.getElementsByName("month");
        if(year[0].value !== "0" && month[0].value !== "0"){
        // DO your redirect
        alert(year[0].value+'-'+month[0].value);
        }
        }


        https://jsfiddle.net/4d9phtgv/






        share|improve this answer




























          3














          I would have chosen to use jquery to catch the change event (it's just what I'm used to). But my guess is that you want to keep your vanilla JS approach, you could write an eventlistener in javascript instead of using the onchange attribute. But here is a solution that keeps to how you work right now. Just add the javascript in a script tag in the DOM.



          function customSelectChange() {
          var year = document.getElementsByName("year");
          var month = document.getElementsByName("month");
          if(year[0].value !== "0" && month[0].value !== "0"){
          // DO your redirect
          alert(year[0].value+'-'+month[0].value);
          }
          }


          https://jsfiddle.net/4d9phtgv/






          share|improve this answer


























            3












            3








            3







            I would have chosen to use jquery to catch the change event (it's just what I'm used to). But my guess is that you want to keep your vanilla JS approach, you could write an eventlistener in javascript instead of using the onchange attribute. But here is a solution that keeps to how you work right now. Just add the javascript in a script tag in the DOM.



            function customSelectChange() {
            var year = document.getElementsByName("year");
            var month = document.getElementsByName("month");
            if(year[0].value !== "0" && month[0].value !== "0"){
            // DO your redirect
            alert(year[0].value+'-'+month[0].value);
            }
            }


            https://jsfiddle.net/4d9phtgv/






            share|improve this answer













            I would have chosen to use jquery to catch the change event (it's just what I'm used to). But my guess is that you want to keep your vanilla JS approach, you could write an eventlistener in javascript instead of using the onchange attribute. But here is a solution that keeps to how you work right now. Just add the javascript in a script tag in the DOM.



            function customSelectChange() {
            var year = document.getElementsByName("year");
            var month = document.getElementsByName("month");
            if(year[0].value !== "0" && month[0].value !== "0"){
            // DO your redirect
            alert(year[0].value+'-'+month[0].value);
            }
            }


            https://jsfiddle.net/4d9phtgv/







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 12:56









            PatrickPatrick

            220311




            220311

























                3














                With delay






                const month = document.getElementById('month');
                const year = document.getElementById('year');

                function handleChange() {
                if (month.value && year.value) {
                const urlSearch = new URLSearchParams();
                urlSearch.append('month', month.value);
                urlSearch.append('year', year.value);
                console.log(urlSearch.toString());
                }
                }

                <select onchange="handleChange()" name="month" id="month">
                <option value="">Choice month</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                </select>
                <select onchange="handleChange()" name="year" id="year">
                <option value="">Choice year</option>
                <option value="2001">2001</option>
                <option value="2002">2002</option>
                <option value="2003">2003</option>
                <option value="2004">2004</option>
                <option value="2005">2005</option>
                <option value="2006">2006</option>
                <option value="2007">2007</option>
                <option value="2008">2008</option>
                <option value="2009">2009</option>
                <option value="2010">2010</option>
                <option value="2011">2011</option>
                <option value="2012">2012</option>
                </select>








                share|improve this answer




























                  3














                  With delay






                  const month = document.getElementById('month');
                  const year = document.getElementById('year');

                  function handleChange() {
                  if (month.value && year.value) {
                  const urlSearch = new URLSearchParams();
                  urlSearch.append('month', month.value);
                  urlSearch.append('year', year.value);
                  console.log(urlSearch.toString());
                  }
                  }

                  <select onchange="handleChange()" name="month" id="month">
                  <option value="">Choice month</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                  <option value="7">7</option>
                  <option value="8">8</option>
                  <option value="9">9</option>
                  <option value="10">10</option>
                  <option value="11">11</option>
                  <option value="12">12</option>
                  </select>
                  <select onchange="handleChange()" name="year" id="year">
                  <option value="">Choice year</option>
                  <option value="2001">2001</option>
                  <option value="2002">2002</option>
                  <option value="2003">2003</option>
                  <option value="2004">2004</option>
                  <option value="2005">2005</option>
                  <option value="2006">2006</option>
                  <option value="2007">2007</option>
                  <option value="2008">2008</option>
                  <option value="2009">2009</option>
                  <option value="2010">2010</option>
                  <option value="2011">2011</option>
                  <option value="2012">2012</option>
                  </select>








                  share|improve this answer


























                    3












                    3








                    3







                    With delay






                    const month = document.getElementById('month');
                    const year = document.getElementById('year');

                    function handleChange() {
                    if (month.value && year.value) {
                    const urlSearch = new URLSearchParams();
                    urlSearch.append('month', month.value);
                    urlSearch.append('year', year.value);
                    console.log(urlSearch.toString());
                    }
                    }

                    <select onchange="handleChange()" name="month" id="month">
                    <option value="">Choice month</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    </select>
                    <select onchange="handleChange()" name="year" id="year">
                    <option value="">Choice year</option>
                    <option value="2001">2001</option>
                    <option value="2002">2002</option>
                    <option value="2003">2003</option>
                    <option value="2004">2004</option>
                    <option value="2005">2005</option>
                    <option value="2006">2006</option>
                    <option value="2007">2007</option>
                    <option value="2008">2008</option>
                    <option value="2009">2009</option>
                    <option value="2010">2010</option>
                    <option value="2011">2011</option>
                    <option value="2012">2012</option>
                    </select>








                    share|improve this answer













                    With delay






                    const month = document.getElementById('month');
                    const year = document.getElementById('year');

                    function handleChange() {
                    if (month.value && year.value) {
                    const urlSearch = new URLSearchParams();
                    urlSearch.append('month', month.value);
                    urlSearch.append('year', year.value);
                    console.log(urlSearch.toString());
                    }
                    }

                    <select onchange="handleChange()" name="month" id="month">
                    <option value="">Choice month</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    </select>
                    <select onchange="handleChange()" name="year" id="year">
                    <option value="">Choice year</option>
                    <option value="2001">2001</option>
                    <option value="2002">2002</option>
                    <option value="2003">2003</option>
                    <option value="2004">2004</option>
                    <option value="2005">2005</option>
                    <option value="2006">2006</option>
                    <option value="2007">2007</option>
                    <option value="2008">2008</option>
                    <option value="2009">2009</option>
                    <option value="2010">2010</option>
                    <option value="2011">2011</option>
                    <option value="2012">2012</option>
                    </select>








                    const month = document.getElementById('month');
                    const year = document.getElementById('year');

                    function handleChange() {
                    if (month.value && year.value) {
                    const urlSearch = new URLSearchParams();
                    urlSearch.append('month', month.value);
                    urlSearch.append('year', year.value);
                    console.log(urlSearch.toString());
                    }
                    }

                    <select onchange="handleChange()" name="month" id="month">
                    <option value="">Choice month</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    </select>
                    <select onchange="handleChange()" name="year" id="year">
                    <option value="">Choice year</option>
                    <option value="2001">2001</option>
                    <option value="2002">2002</option>
                    <option value="2003">2003</option>
                    <option value="2004">2004</option>
                    <option value="2005">2005</option>
                    <option value="2006">2006</option>
                    <option value="2007">2007</option>
                    <option value="2008">2008</option>
                    <option value="2009">2009</option>
                    <option value="2010">2010</option>
                    <option value="2011">2011</option>
                    <option value="2012">2012</option>
                    </select>





                    const month = document.getElementById('month');
                    const year = document.getElementById('year');

                    function handleChange() {
                    if (month.value && year.value) {
                    const urlSearch = new URLSearchParams();
                    urlSearch.append('month', month.value);
                    urlSearch.append('year', year.value);
                    console.log(urlSearch.toString());
                    }
                    }

                    <select onchange="handleChange()" name="month" id="month">
                    <option value="">Choice month</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    </select>
                    <select onchange="handleChange()" name="year" id="year">
                    <option value="">Choice year</option>
                    <option value="2001">2001</option>
                    <option value="2002">2002</option>
                    <option value="2003">2003</option>
                    <option value="2004">2004</option>
                    <option value="2005">2005</option>
                    <option value="2006">2006</option>
                    <option value="2007">2007</option>
                    <option value="2008">2008</option>
                    <option value="2009">2009</option>
                    <option value="2010">2010</option>
                    <option value="2011">2011</option>
                    <option value="2012">2012</option>
                    </select>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 '18 at 13:38









                    eustatoseustatos

                    368312




                    368312























                        0














                        You can use a form and submit it to refresh the page with the info, to add the extra fields you can use hidden inputs.






                        monthEnd = function(year, month) {

                        date = new Date(year, month - 1, 1);

                        var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                        return lastDay.getDate();
                        }

                        appendHidden = function(formId, name, value) {
                        $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
                        }

                        begining = function() {
                        year = $('#selectYear').val();
                        month = $('#selectMonth').val();

                        return year + '-' + month + '-' + '1'
                        }

                        end = function() {
                        year = $('#selectYear').val();
                        month = $('#selectMonth').val();

                        return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
                        }

                        $( document ).ready(function() {
                        months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

                        for(i=0; i< months.length; i++) {
                        $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
                        }


                        for(y=2000; y<= 2023;y++) {
                        $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
                        }


                        $('#selectMonth').on('change', function() {
                        appendHidden('form', 'courses', 'on');
                        appendHidden('form', 'events', 'on');
                        appendHidden('form', 'date-from', begining());
                        appendHidden('form', 'date-from', end());
                        $('#selectYear').remove();
                        $('#selectMonth').remove();
                        //$('#form').submit(); Uncomment this line to submit and refresh
                        alert($('#form').serialize())
                        });



                        });

                        .hidden {
                        display: none;
                        }

                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                        <form id="form" action="" method="GET">
                        <div id="custom-select1" class="custom-select" style="width:200px;" >
                        <select id="selectMonth" name="month" >
                        </select>
                        </div>
                        <div class="custom-select" style="width:200px;">
                        <select id="selectYear" name="year" ></select>
                        </div>
                        </form>








                        share|improve this answer






























                          0














                          You can use a form and submit it to refresh the page with the info, to add the extra fields you can use hidden inputs.






                          monthEnd = function(year, month) {

                          date = new Date(year, month - 1, 1);

                          var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                          return lastDay.getDate();
                          }

                          appendHidden = function(formId, name, value) {
                          $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
                          }

                          begining = function() {
                          year = $('#selectYear').val();
                          month = $('#selectMonth').val();

                          return year + '-' + month + '-' + '1'
                          }

                          end = function() {
                          year = $('#selectYear').val();
                          month = $('#selectMonth').val();

                          return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
                          }

                          $( document ).ready(function() {
                          months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

                          for(i=0; i< months.length; i++) {
                          $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
                          }


                          for(y=2000; y<= 2023;y++) {
                          $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
                          }


                          $('#selectMonth').on('change', function() {
                          appendHidden('form', 'courses', 'on');
                          appendHidden('form', 'events', 'on');
                          appendHidden('form', 'date-from', begining());
                          appendHidden('form', 'date-from', end());
                          $('#selectYear').remove();
                          $('#selectMonth').remove();
                          //$('#form').submit(); Uncomment this line to submit and refresh
                          alert($('#form').serialize())
                          });



                          });

                          .hidden {
                          display: none;
                          }

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                          <form id="form" action="" method="GET">
                          <div id="custom-select1" class="custom-select" style="width:200px;" >
                          <select id="selectMonth" name="month" >
                          </select>
                          </div>
                          <div class="custom-select" style="width:200px;">
                          <select id="selectYear" name="year" ></select>
                          </div>
                          </form>








                          share|improve this answer




























                            0












                            0








                            0







                            You can use a form and submit it to refresh the page with the info, to add the extra fields you can use hidden inputs.






                            monthEnd = function(year, month) {

                            date = new Date(year, month - 1, 1);

                            var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                            return lastDay.getDate();
                            }

                            appendHidden = function(formId, name, value) {
                            $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
                            }

                            begining = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + '1'
                            }

                            end = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
                            }

                            $( document ).ready(function() {
                            months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

                            for(i=0; i< months.length; i++) {
                            $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
                            }


                            for(y=2000; y<= 2023;y++) {
                            $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
                            }


                            $('#selectMonth').on('change', function() {
                            appendHidden('form', 'courses', 'on');
                            appendHidden('form', 'events', 'on');
                            appendHidden('form', 'date-from', begining());
                            appendHidden('form', 'date-from', end());
                            $('#selectYear').remove();
                            $('#selectMonth').remove();
                            //$('#form').submit(); Uncomment this line to submit and refresh
                            alert($('#form').serialize())
                            });



                            });

                            .hidden {
                            display: none;
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <form id="form" action="" method="GET">
                            <div id="custom-select1" class="custom-select" style="width:200px;" >
                            <select id="selectMonth" name="month" >
                            </select>
                            </div>
                            <div class="custom-select" style="width:200px;">
                            <select id="selectYear" name="year" ></select>
                            </div>
                            </form>








                            share|improve this answer















                            You can use a form and submit it to refresh the page with the info, to add the extra fields you can use hidden inputs.






                            monthEnd = function(year, month) {

                            date = new Date(year, month - 1, 1);

                            var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                            return lastDay.getDate();
                            }

                            appendHidden = function(formId, name, value) {
                            $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
                            }

                            begining = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + '1'
                            }

                            end = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
                            }

                            $( document ).ready(function() {
                            months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

                            for(i=0; i< months.length; i++) {
                            $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
                            }


                            for(y=2000; y<= 2023;y++) {
                            $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
                            }


                            $('#selectMonth').on('change', function() {
                            appendHidden('form', 'courses', 'on');
                            appendHidden('form', 'events', 'on');
                            appendHidden('form', 'date-from', begining());
                            appendHidden('form', 'date-from', end());
                            $('#selectYear').remove();
                            $('#selectMonth').remove();
                            //$('#form').submit(); Uncomment this line to submit and refresh
                            alert($('#form').serialize())
                            });



                            });

                            .hidden {
                            display: none;
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <form id="form" action="" method="GET">
                            <div id="custom-select1" class="custom-select" style="width:200px;" >
                            <select id="selectMonth" name="month" >
                            </select>
                            </div>
                            <div class="custom-select" style="width:200px;">
                            <select id="selectYear" name="year" ></select>
                            </div>
                            </form>








                            monthEnd = function(year, month) {

                            date = new Date(year, month - 1, 1);

                            var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                            return lastDay.getDate();
                            }

                            appendHidden = function(formId, name, value) {
                            $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
                            }

                            begining = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + '1'
                            }

                            end = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
                            }

                            $( document ).ready(function() {
                            months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

                            for(i=0; i< months.length; i++) {
                            $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
                            }


                            for(y=2000; y<= 2023;y++) {
                            $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
                            }


                            $('#selectMonth').on('change', function() {
                            appendHidden('form', 'courses', 'on');
                            appendHidden('form', 'events', 'on');
                            appendHidden('form', 'date-from', begining());
                            appendHidden('form', 'date-from', end());
                            $('#selectYear').remove();
                            $('#selectMonth').remove();
                            //$('#form').submit(); Uncomment this line to submit and refresh
                            alert($('#form').serialize())
                            });



                            });

                            .hidden {
                            display: none;
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <form id="form" action="" method="GET">
                            <div id="custom-select1" class="custom-select" style="width:200px;" >
                            <select id="selectMonth" name="month" >
                            </select>
                            </div>
                            <div class="custom-select" style="width:200px;">
                            <select id="selectYear" name="year" ></select>
                            </div>
                            </form>





                            monthEnd = function(year, month) {

                            date = new Date(year, month - 1, 1);

                            var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                            return lastDay.getDate();
                            }

                            appendHidden = function(formId, name, value) {
                            $('#' + formId).append('<input type="text" name="' + name + '" class="hidden" value="' + value + '" />');
                            }

                            begining = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + '1'
                            }

                            end = function() {
                            year = $('#selectYear').val();
                            month = $('#selectMonth').val();

                            return year + '-' + month + '-' + monthEnd(parseInt(year), parseInt(month));
                            }

                            $( document ).ready(function() {
                            months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

                            for(i=0; i< months.length; i++) {
                            $('#selectMonth').append('<option value="' + (i + 1) + '">' + months[i] + '</option>');
                            }


                            for(y=2000; y<= 2023;y++) {
                            $('#selectYear').append('<option value="' + y + '">' + y + '</option>');
                            }


                            $('#selectMonth').on('change', function() {
                            appendHidden('form', 'courses', 'on');
                            appendHidden('form', 'events', 'on');
                            appendHidden('form', 'date-from', begining());
                            appendHidden('form', 'date-from', end());
                            $('#selectYear').remove();
                            $('#selectMonth').remove();
                            //$('#form').submit(); Uncomment this line to submit and refresh
                            alert($('#form').serialize())
                            });



                            });

                            .hidden {
                            display: none;
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <form id="form" action="" method="GET">
                            <div id="custom-select1" class="custom-select" style="width:200px;" >
                            <select id="selectMonth" name="month" >
                            </select>
                            </div>
                            <div class="custom-select" style="width:200px;">
                            <select id="selectYear" name="year" ></select>
                            </div>
                            </form>






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 26 '18 at 14:21

























                            answered Nov 26 '18 at 14:14









                            vladwoguervladwoguer

                            3181310




                            3181310






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480727%2fjavascript-refresh-same-page-with-query-string-from-selects%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







                                Popular posts from this blog

                                Wiesbaden

                                Marschland

                                Dieringhausen