houssam_ballout Posted February 9, 2006 Share Posted February 9, 2006 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 More sharing options...
scott100 Posted February 9, 2006 Share Posted February 9, 2006 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.<{POST_SNAPBACK}> 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 More sharing options...
houssam_ballout Posted February 9, 2006 Author Share Posted February 9, 2006 document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value; I mean this, just can u explain the syntax please? Link to comment Share on other sites More sharing options...
aspnetguy Posted February 9, 2006 Share Posted February 9, 2006 document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value; I mean this, just can u explain the syntax please?<{POST_SNAPBACK}> 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 More sharing options...
scott100 Posted February 9, 2006 Share Posted February 9, 2006 document.bgColor=document.list.colors.options[document.list.colors.selectedIndex].value; I mean this, just can u explain the syntax please?<{POST_SNAPBACK}> 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 More sharing options...
aspnetguy Posted February 9, 2006 Share Posted February 9, 2006 Hope this made sence Well it makes more sense than my pathetic attempt to explain it. lol Link to comment Share on other sites More sharing options...
scott100 Posted February 9, 2006 Share Posted February 9, 2006 Hope this made senceĀ <{POST_SNAPBACK}> Well it makes more sense than my pathetic attempt to explain it. lol<{POST_SNAPBACK}> That wasnt a dig a you by the way, i think we were both replying to the same problem at the same time... Link to comment Share on other sites More sharing options...
aspnetguy Posted February 9, 2006 Share Posted February 9, 2006 That wasnt a dig a you by the way, i think we were both replying to the same problem at the same time... <{POST_SNAPBACK}> I know...I didn't take it that way...I just understood your explaination more after reading mine agina Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now