This is my JavaScript code:
<script>
function change(name){
var element = document.all.name.value;
}
</script>
It returns an error. How to pass to the function the element’s name in order to change its value?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For a quick fix:
But keep in mind this gets the value, not the element itself. To get the element, then change the value, do:
Also, you should consider using another way to access the elements instead of
document.all. Look into using IDs instead of names with thedocument.getElementById()function. See this JavaScript best practices list.As Peter Bailey points out in his answer, many HTML elements can have the same name. This is one reason why
document.getElementById()is preferable: element IDs are unique (in valid HTML, of course).