How can I set the default value for an HTML element?

I thought that adding a “value” attribute set on the <select> element below would cause the <option> containing my provided “value” to be selected by default: <select name=”hall” id=”hall” value=”3″> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> However, this did not work as I had expected. How can I set which <option> element is selected by … Read more

How do I make a placeholder for a ‘select’ box?

I’m using placeholders for text inputs which is working out just fine. But I’d like to use a placeholder for my selectboxes as well. Of course I can just use this code: <select> <option value=””>Select your option</option> <option value=”hurr”>Durr</option> </select> But the ‘Select your option’ is in black instead of lightgrey. So my solution could … Read more

Get selected value in dropdown list using JavaScript

How do I get the selected value from a dropdown list using JavaScript? I tried the methods below, but they all return the selected index instead of the value: var e = document.getElementById(“ddlViewBy”); function show(){ var as = document.forms[0].ddlViewBy.value; var strUser = e.options[e.selectedIndex].value; console.log(as, strUser); } e.onchange=show; show(); <form> <select id=”ddlViewBy”> <option value=”1″>test1</option> <option value=”2″ … Read more