Hi. I am suppose to create a variable named allSelect that references all of the select elements in my page, then for each item within the allSelect collection, I am suppose to add an onchange event handler that runs a function called loadLink().
I am getting an error:
Webpage error details
Message: 'document.forms.0.allSelect' is null or not an object
(I do not understand, because I have select elements on the page)
Here is my code for the javascript file:
window.onload = init; //We add an event handler so the init function is called upon the page loading
function init() {
var allSelect = document.getElementsByTagName("select");
alert(allSelect.length);
I am supposed to create a loadLink() function that will cause the browser to load a URL from a selection list. I also need to create a variable named sIndex that points to the index of the selected option in the current selection list. I am supposed to use the THIS keyword to reference the current selection list.
I am sorry but I am very new to Javascripting. can you please clarify at bit more "document.forms[0] does not have a property called 'allSelect'.
The phrase was no a happy one. It is true that the form has no property called 'allSelect', but it might have some (one or more) elements which bear the name="allSelect". In JavaScript there is no firm distinction between objects and properties, as long as any object can be treated as the property of its parentNode (if it is a HTML element) or simply a property of another object. In fact, in JavaScript everything is an object, but this is another discussion.
Now, your case: please post some code to see what is all about.
The phrase was no a happy one. It is true that the form has no property called 'allSelect', but it might have some (one or more) elements which bear the name="allSelect". In JavaScript there is no firm distinction between objects and properties, as long as any object can be treated as the property of its parentNode (if it is a HTML element) or simply a property of another object. In fact, in JavaScript everything is an object, but this is another discussion.
Now, your case: please post some code to see what is all about.
I have attached all the related files, I need to accomplish the following:-
I need to create init() function and need to write an event handler to run the init() function when the page is loaded. Within this init() function, I need to createa variable named allSelect that references all of the selection elements in the document.
For each item within allSelect Object collection, I need to an onchange event handler that will run the LoadLink() function when the selection list changes.
I need to create a loadLink() function that will cause the web browser to load a URL from a selection list. The following commands needs to be added to the function
1) A variable named sIndex that points to the index of the selected option in the current selection list. Its preferred to use the THIS keyword to reference the current selection.
2) Web pages can be loaded by using the command location.href = url;
Thanks everyone, I have decided to read a bit more and give another try. I hope to solve it myself, the only thing is that I need to read a bit more in detail.
Bookmarks