Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select dropdown value by a-tag button

On my page https://vidsk.dk/, I have a form that has a dropdown menu with 9 options. I also have nine corresponding buttons with a-tags. I want the user to be able to press the button and thereby select the corresponding value in the form dropdown menu.

My options:

<select>
<option value="Dansk">Dansk</option>
<option value="Engelsk">Engelsk</option>
<option value="Tysk">Tysk</option>
<option value="Fysik">Fysik</option>
<option value="Matematik">Matematik</option>
<option value="Samfundsfag">Samfundsfag</option>
<option value="Latin">Latin</option>
<option value="Spansk">Spansk</option>
<option value="Kinesisk">Kinesisk</option>
</select>

Example of an a-tag:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<a>Englsk</a>

Purpose:
The user sees 9 big buttons and wants to click on his selection, which will change the form and nudge the user toward filling the rest of the form and sending it.

Page background:
The page is made in WordPress which for some reason uses the a-tag rather than a button-tag for buttons. I would prefer not to have to replace the tag.

My Background:
I am fairly versed in HTML and CSS and used to be okay with PHP, but that is 10 years ago. 100% self-taught. If a solution exists with only PHP, i.e., without Java or the like, I would prefer such. I have done extensive searches on this (2 hours+) but cannot find a solution.

>Solution :

PHP is a server side language so you cannot change the selected value without rerendering the whole page which wouldn’t be ideal.

You need to use JavaScript.

You can change the value of the dropdown by modifying the ‘value’ attribute.

function setSelect(val){
  document.getElementById("myselect").value = val;
}
<select id="myselect">
  <option value="Dansk">Dansk</option>
  <option value="Engelsk">Engelsk</option>
  <option value="Tysk">Tysk</option>
  <option value="Fysik">Fysik</option>
  <option value="Matematik">Matematik</option>
  <option value="Samfundsfag">Samfundsfag</option>
  <option value="Latin">Latin</option>
  <option value="Spansk">Spansk</option>
  <option value="Kinesisk">Kinesisk</option>
</select>

<div>
  <button onclick="setSelect('Latin')">Latin</button>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading