I have a js function which controls the appearance of a select element upon change. However, the one thing i need is for the code to run from start so that when the element loads the selected options class should have already been applied.
css:
select {height: 50px; width: 80px; border: solid 1px #c8c8c8; color:rgba(0, 0, 0, 0);}
select:focus, #select:focus {
color:black;
}
.lightgrey {background: lightgrey}
.green {background:green}
.orange {background:orange}
.yellow {background:yellow}
.red {background:red}
.purple {background:purple}
JavaScript:
function colourFunction(select) {
var colour = select.options[select.selectedIndex].className;
select.className = colour;
select.blur();
}
Select:
<select class="selectElement" runat="server" id="dropdown_" onchange="colourFunction(this)">
<option selected="selected" class="lightgrey" value="N">N</option>
<option class="green" value="G">G</option>
<option class="orange" value="O">O</option>
<option class="yellow" value="A">A</option>
<option class="red" value="R">R</option>
<option class="purple" value="U">U</option>
</select>
I want the class for N to be applied on load with color:rgba(0, 0, 0, 0);
onload runs after page load and all javascript is available, so the codeAddress() function can be declared anywhere within the page or linked js files. It doesn't have to come before unless it were called during the page load itself.
Example Explained Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.
if you're using jquery:
$(function(){
//all onload actions you want
});
if you want to stick with pure js
document.onreadystatechange=function(){
//all onload actions you want
}
If you're wanting the JS to run on page load, then you can use the "window.onload" command.
window.onload = function()
{
colourFunction(select);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With