0

I am trying to redirect to url when a select is changed and add option text in url query parameter, I have tried this but it seems not working:

$(document).ready(function () {
            var url = window.location.href;
            $("#select-opt").change(function() {
                var option = $(this).find('option:selected');
                url = option.data("url");
            });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<select id="select-opt" onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
        <option value="test-select1.html">Value 1</option>
        <option value="test-select2.html">Value 2</option>
        <option value="test-select3.html">Value 3</option>
    </select>
Sanjeev Kumar
  • 2,897
  • 2
  • 27
  • 62
  • I don't think it's helping that you're mixing inline script (in the `onchange`) and the jQuery script. Try doing everything everything in the jQuery. Also, setting the `window.location.href` into a variable and then updating the variable does **not** update the `window.location.href` – freefaller Oct 11 '18 at 08:28
  • You need to take the value of your selected option for your redirect, such as "option.val()" or "$(this).find('option:selected').val()". This you need to use with "window.location.href = " instead setting the url. – Daniel Frech Oct 11 '18 at 08:30

3 Answers3

1

Setting window.location.href to a variable, and then updating the variable will not set window.location.href

I've moved your onchange code into the jquery and fixed the error.

I've also added the code to add the text from the option to the query string.

$(document).ready(function () {
  $("#select-opt").change(function() {
    var $option = $(this).find(':selected');
    var url = $option.val();
    if (url != "") {
      url += "?text=" + encodeURIComponent($option.text());
      // Show URL rather than redirect
      $("#output").text(url);
      //window.location.href = url;
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<select id="select-opt">
  <option value="">Select one</option>
  <option value="test-select1.html">Value 1</option>
  <option value="test-select2.html">Value 2</option>
  <option value="test-select3.html">Value 3</option>
</select>
<div id="output"></div>
freefaller
  • 17,790
  • 6
  • 50
  • 80
0

I fixed your code:

$(document).ready(function () {
    $("#select-opt").change(function() {
        window.location.href = $(this).val();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<select id="select-opt" onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
        <option value="test-select1.html">Value 1</option>
        <option value="test-select2.html">Value 2</option>
        <option value="test-select3.html">Value 3</option>
    </select>
Shubham Baranwal
  • 2,333
  • 2
  • 12
  • 23
Jack Bashford
  • 38,499
  • 10
  • 36
  • 67
  • Thanks for the answer but on select change I also wanted to send `Value 1` in url query parameter or whichever is selected – Sanjeev Kumar Oct 11 '18 at 08:30
  • For adding query parameter in URL you can refer this [stackoverflow](https://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript). @SanjeevKumar – Shubham Baranwal Oct 11 '18 at 08:34
0

You can do the following:

$('#select-opt').on('change', function() {
    var selectedOption = $(this).val(); //or use this: $(this).find(':selected').val();
    var parameter = '?text=';
    var parameterValue = $(this).find(':selected').text();
    var url = selectedOption+parameter+parameterValue;
    if(selectedOption != "") {
        alert(url);
        //window.location.href=''+url;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select-opt">
    <option value="">Select value</option>
    <option value="test-select1.html">Value 1</option>
    <option value="test-select2.html">Value 2</option>
    <option value="test-select3.html">Value 3</option>
</select>

You were actually really close. What you need is to store the value of your select as your url path, but leave the window.location.href as its own function, and simply put the url variable at the end of it.

right now I am just alerting the path for testing / visual purposes in this example. If you remove the alert and out-comment the line below, you would get your desired redirect instead.

Alternatively, you can use

$(this).find(':selected').val()

instead of

$(this).val()

for your selected option value. Depends what version of jQuery you run with I believe.

Martin
  • 1,849
  • 1
  • 11
  • 17