Dede1989
Dede1989

Reputation: 53

Passing parameters on onChange of html select

lets say i have got two html pages which both contain the same select box:

<select onchange="this.options[this.selectedIndex].value && (window.location =    this.options[this.selectedIndex].value);">
<option value="/site1.php">Site1</option>
<option value="/site2.php">Site2</option>
</select>

I am looking for a way to pass the value of selectedIndex to the next page, so I can use it at the second page select box too. Any suggestions?

Thanks in advance

Upvotes: 1

Views: 1779

Answers (2)

nicael
nicael

Reputation: 18995

localStorage

Store:

localStorage.userEdits=this.options[this.selectedIndex].value;

Get, on the other page :

var yourIndex = localStorage.userEdits;

hash

Set:

window.location.href="yourURL#index="+this.options[this.selectedIndex].value;

Get:

var yourIndex=window.location.hash.replace("index=","").parseInt();

Upvotes: 1

Jenian
Jenian

Reputation: 592

You can use query parameters:

window.location = 'YOUR_URL?selectedIndex=' + this.options[this.selectedIndex].value

Using query params forces you to parse them. This is explained here (you can do just fine with pure JS).

Upvotes: 0

Related Questions