You have 2 errors in your code if I understand what you are trying to do correctlly
1) When a web page has multiple elemenst of the same type, like multiple input elements, they will be stored in the dom as arrays named the element.
Therefore your DOM will have input[0] for the first input element, input[1] for the second and so on.
Your code has names assigned to the input elements with a value of 'input' as well. So there is a conflict in the DOM there. I wouldn't assign names to elements with the value the same as the element type.
2) for javascript execution you don't need the [] in element names even if they are the same in multiple elements. The DOM will automatically put element names into an array if it detects the same name in multiple elements.
I have changed your input element names from input[] to input1 in the code below and your javascript now works as I think you want it to.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function getthis(form, test) {
//form.justinput.value="foobar" // works
//document.getElementById('justinput').value="foobar" // works
form.input1[0].value="foobar" // original fails *****NOW THIS WORKS ********
//document.getElementById('input[0]').value="foobar" // fails
}
</script>
</head>
<body>
<form method="post">
<input type=checkbox name=check>
<input type=text name=justinput value=testing>
<hr>
<input type=checkbox name=check>
<input type=text name=input1 value=foo>
<hr>
<input type=checkbox name=check onclick="getthis(form, this)">
<input type=text name=input1 value=bar>
</form>
</body>
</html>
I think you confused "justinput" with "input". I never had two different types of "input". Nevertheless, thanks for the discovery about the lack of need of "[]". I never imagined it wasn't even needed. But like you admitted yourself, now document.getElementByName doesn't work. Isn't there a way to make it support your way too?
Also, the question still remains, how do I, say, automatically make the first checkmark control the first input field, and the second checkmark to control the second input field (and the hundredth checkmark to control the hundredth input field, etc.)? In other words, control the matching input field of the same index.
I think you confused "justinput" with "input". I never had two different types of "input". Nevertheless, thanks for the discovery about the lack of need of "[]". .
I didn't say you had 2 types of inputs.
What I said was you had an array of input names called input which was possibly conflicting with the DOM's array called input which stores all the input elements in a web page.
In your original code
Code:
<input type=text name=input[] value=bar>
I wouldn't name the elements 'input' because of the potential conflict I mentioned above.
In your original code, the line that you commented 'failed' now works and is highlighted in green. All I did was change the name of the element to input1
The brackets are required to send array data to server programs.
Oh yeah, now I remembered why I used brackets in the first place!
Thanks tirna for the clarification about input vs. input1. Though I just used "input" for this example, I'll remember from now on it may cause problems.
Okay, I assume the nextSibling method is not meant for document.getElementByName. With that said, here's how to get over the whitespace issue:
Please allow more edit time, as the previous post could have been replaced by the following. I've programmed a way to control the amount of next/previous in next/previousSibling:
Okay, I assume the nextSibling method is not meant for document.getElementByName. With that said, here's how to get over the whitespace issue
nextSibling has no restriction when using getElementByName.
I gave an example of how to avoid the whitespace issue. Place the 2 inputs next to each other without whitespace between them.
At least 98% of internet users' DNA is identical to that of chimpanzees
Bookmarks