Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  86] [ 6]  / answers: 1 / hits: 8366  / 9 Years ago, mon, february 16, 2015, 12:00:00

How can I have my dropdown selected with an url parameter?
I found this:
Dropdown selected based on URL parameter - PHP or jQuery?



But it does not work for me. What am I doing wrong?
My url would be:



kontakt.php?Betreff=3


script:



var val = location.href.match(/[?&]Betreff=(.*?)[$&]/)[1];   // get params from URL
$('#Betreff').val(val); // assign URL param to select field


and:



<select class=mailstyle name=Betreff id=Betreff>
<option value=1>First</option>
<option value=2>Second</option>
<option value=3>Third</option>
</select>

More From » php

 Answers
4

Your regex is incorrect. You can use this function (from this question) to get the parameter value:



function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,])[1].replace(/+/g, '%20'))||null
}


You can then use that to set the value:



var val = getURLParameter('Betreff');
$('#Betreff').val(val); // assign URL param to select field

[#39230] Saturday, February 14, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellisw

Total Points: 625
Total Questions: 92
Total Answers: 88

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
ellisw questions
Mon, Aug 23, 21, 00:00, 3 Years ago
Fri, Nov 20, 20, 00:00, 4 Years ago
Sat, Jun 20, 20, 00:00, 4 Years ago
Tue, Apr 21, 20, 00:00, 4 Years ago
;