Jump to content

javascript problem


houssam_ballout

Recommended Posts

Hello all, please can u tell me what is the problem with my code?Also, please can some one explain for me the implemented function (that code is from the w3schools)?thanks.<html><body><script language="javascript"> function change() { document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value; }</script> <p>My first background changer:<form name="list"> <select name="colors"> <option value="red">Red <option value="blue">Blue <option value="green">Green <option value="orange">Orange <option value="white">White </select> <input type="button" value="Change Background color!" name="myButton" onClick="change()" /></form></p></body></html>

Link to comment
Share on other sites

Hello all, please can u tell me what is the problem with my code?Also, please can some one explain for me the implemented function (that code is from the w3schools)?thanks.

Your code worked ok for me...you may need to put the following code all on the same line as opposed to two:
document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value;

<input type="button" value="Change Background color!" name="myButton" onClick="change()" />

Are you also asking what does the function do? :) It obtains the value of the option chosen from the select list ie blue, then changes the documents background to that colour.

Link to comment
Share on other sites

document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value; I mean this, just can u explain the syntax please?

document.bgColor is the background color of the pagedocument.list is the form in the page called list.colors is the name of the drop down list.options references the options array in the <select>[document.list.colors.selectedIndex] is hte selected option of the select which is the index number of the value to get form the options array.value is the value of the selected option
Link to comment
Share on other sites

document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value; I mean this, just can u explain the syntax please?

ok document.bgColor is built into javascript for changing the background colour, you could simply say something like document.bgColor="blue";You are then using the DOM to access the value of document.list.colors.options, if you look at the code in the body, notice the use of names<form name="list"> <select name="colors"><option value="red">Red You are working your way down the document and accessing individual elements ie the form then the list then the option.[document.list.colors.selectedIndex] selectIndex is a javascript built in array used for accessing the values of a <select> tag.Hope this made sence :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...