ubuntujavy
ubuntujavy

Reputation: 55

Add string and input select value to url onchange and page load

I have a code below that adds a string along with the value of a input select when the page is loaded. The code below somewhat works but there's long unrecognizable string after the = sign.
I also need for the code to load when the page is loaded.
In other word, the url should also includes the appended text when the page is loaded.
Can anyone help?

<script>
  $(document).ready(function() {
    $("#staff-list").on('change', function() {
      location.href = "?account=" + $(this).val;
    });
  });
</script>

Upvotes: 1

Views: 1749

Answers (3)

Nirav Madariya
Nirav Madariya

Reputation: 1508

You should change the line, where you append URL from,

location.href = "?account=" + $(this).val; to,
location.href = "?account=" + $(this).val();

and you want your code to return the appended URL, you can do like,

    <script>
      $(document).ready(function() {
        $("#staff-list").on('change', function() {
           $("#id_of_a").attr("href","somefile.php?account=" + $(this.val());
        });
      });
    </script>

I'm not sure what your code scenario is, but this might help.

Upvotes: 3

Ram Manoj
Ram Manoj

Reputation: 21

.val should be changed to .val() and you are using .ready(). So document.ready() event will be called once the DOM is loaded which is earlier than body.onload().

You are using a change event function which actually triggers the action. Maybe would like to revisit your function.

If you want to store the URL and access it again in other pages. Use cookies.

Upvotes: 0

user8016859
user8016859

Reputation:

.val is a function so it should be .val()

location.href = "?account=" + $(this).val();

Upvotes: 1

Related Questions