Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I trigger the onChange when the page loads

Status
Not open for further replies.

ade67123

Technical User
Sep 8, 2006
3
GB
How can I trigger the onChange when the page loads.

For instance say that the colour red has been inserted in to mysql and when I return to this page the colour red is inserted back in to the drop down menu, how can I get javascript to trigger this when the page loads so that the messages are displayed without having to trigger this function by using the drop down menu?

<script type="text/javascript">

function ShowItem(){
var Index = document.getElementById("mycolor").selectedIndex;

chosenColour = document.getElementById("mycolor").options[Index].text;

document.getElementById("Msg1").innerHTML = "You have chosen the colour " + chosenColour
document.getElementById("Msg2").innerHTML = "I like the colour " + chosenColour
document.getElementById("Msg3").innerHTML = chosenColour + " is a nice colour"
}

</script>

<select id="mycolor" name="mycolor" onchange="ShowItem()">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="White">White</option>
</select>
<BR><BR>
<span id="Msg1"></span>
<BR><BR>
<span id="Msg2"></span>
<BR><BR>
<span id="Msg3"></span>
 
Thank you for your help. Becuase the colours drop down menu where contained in an expanding menu the above did not work for me, I should have told you before I posted the message. However your above example helped solve the problem

Inside the javascript that controlled the expanding menus I entered the following code so that when the menu is expanded the ShowItem function is called.

onclick = function() { ShowItem(); }

Thank you for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top