0

I have the following code to display the last path of previous page.

var ref = document.referrer;
var pElements = ref.replace(/\/$/, '').split('/');
var last = pElements[pElements.length - 1]

Now what i want to do is to change the dropdown-list in the new site according to the value of last since it contains 4 options with values which are equal to last (you can come from page a,b,c or d)

$("[value='last']").attr('selected',true)

I tried this but the dropdown won't change the select according to last

Hopefully you get what i mean… Sorry for my bad english. But i just want to output last in the last code-snipped, like when i come from a the code would look like:

$("[value='a']").attr('selected',true)

and the dropdown would select a

3
  • and use .prop('selected', 'true'); instead '.attr('selected',true)' Commented Oct 2, 2015 at 8:46
  • Does not work - the console outputs [] . The attr-way works when i type in by hand. Could it be case-sensitive? Commented Oct 2, 2015 at 8:50
  • Ohh! It IS Case-sensitive… Now how can i change the output of $last so that the first character would be captial ? Commented Oct 2, 2015 at 8:51

2 Answers 2

3

To set the selected option of a select element, use val(). Try this:

$("select").val(last);

Obviously, you may need to make the selector a little more specific than that to suit your needs.

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

4 Comments

When i do this my Dropdown changes to blank
Does the value in the last variable exactly match the value of an option in your select? Seeing your HTML would help to diagnose the issue.
It was my fault, didn't know that a dropdown is case sensitive in this matter.
hey rory, sorry i've chosen binderbound's answer since he gave me a solution to my followed problem with the case sensitive-thing. Your answer works too, dont get mad pls :)
2

In your selector $("[value='last']") you are not evaluating last, you are literally taking the option that has a value of "last".

Instead, make your selector take the actual value of last.

$("[value='"+last+"']")

Important gotcha - you will need to make sure to make all the other options don't have the selected attribute by clearing that attribute beforehand. jQuery will do this for you if you use .val(last) on the select element.

6 Comments

Hey, can you tell me how to change the first character of my + last + to uppercase? I guess that the dropdown is case sensitive, since it contains options with capital first letters, like Test and not test
I'd recommend changing the value you need so that the first character is lowercase. But if you can't, then you can do last = last[0].toUpperCase() + last.substring(1) before you use it in your selector. This assumes your string is at least 2 characters long, which should be safe enough if the value is always a path.
Woaaa so excited :) The finish is in sight! But you've written ToUpperCase, should be toUpperCase
I'm chosing your answer as the right answer. Wasn't easy to decide since both answers (yours and Rory's) work - but for me your bonus-answer helped me in my situation to get my problem solved! Thanks mate i owe you a beer (next time you come to germany, write me :p )
Haha, sure - next time I'm in the Northern Hemisphere
|

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.