1

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>
3
  • 1
    you might need to change from val; to val();. Commented May 17, 2017 at 4:24
  • One simple change and it works. Thanks, Nirav. BTW, if I want to make this code to return the appended url on page load, how should I do it? Commented May 17, 2017 at 4:29
  • which server-side language you're using?? Commented May 17, 2017 at 4:35

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. How would I change the code to make it to append the url when page is loaded?
you can use onload() function of <body> tag.
Instead of appending the url on onchange, I also wanted it to append url when the page is loaded, meaning when the page is click, it append the url. Does cookies and/or session is necessary for this?
Sorry, I'm new to javascript and am not sure how to call the onload in the body tag. Can you help?
1

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

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

Comments

0

.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.

2 Comments

You're saying that I can't use onchange and onload with the current code?
What you have written will execute on change event of element id #staff-list and not on the url load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.