MMJZ
MMJZ

Reputation: 163

Any way to automatically select a value of a dropdown menu when the page loads?

I got me a fairly simple webpage (I'll refer to it as 'that customising page' from now on) with a few dropdown menus to customise a product. At present, it has two menus, for the shape of the product and the text engraved on it.

I also have pages for each shape with a small description. From those pages, I want to add a link that says 'get this' or something similar, which will take the user to that customising page to personalise what they buy.

The problem is, when they get to that customising page, I want the dropdown menu to automatically select that option for that shape from the page they were just at.

If it's possible with JavaScript that'd help a bunch, because that's all I'm confident with. If I can send some info in the URL, that'd be brilliant, if I knew how to extract it.

(Extra) When a value from the drop menu is selected, it updates a div on the page to display a preview. If it's possible to automatically select a value when the page loads, is it likely to also run the function to update the image, or would I have to add that in elsewhere?

Upvotes: 0

Views: 486

Answers (2)

Newse
Newse

Reputation: 2338

There are a number of ways you can approach this. For example, on the server-side (via PHP, Asp.net, etc..) you can grab the URL parameter, that you set when you redirect to your customizing screen, and select the default option on the drop down before the page is rendered.

You could also just as easily do it on the client-side as well. You just need to grab the parameter from the Url and set the selected option.

Get escaped URL parameter

Upvotes: 1

Thomas Junk
Thomas Junk

Reputation: 5676

Yes. And without any Javascript involved. Plain HTML.

Take a look at the fine documentation at the MDNs and look for the attribute selected in the examples.

Upvotes: 0

Related Questions