I have a script wherein I would like to get the form's placeholder attribute and then use as a conditional in a function like so (this doesn't seem right to me, so I'm not surprised it doesn't work):
if (document.getElementsByName("Name").input.placeholder == "Enter Name") {
//do something
}
The wrapper function actually takes the form as argument:
function form_function(form) {
var input = document.getElementsByName("Name");
var placeholder = input.placeholder;
if (placeholder == "Empty") {
// do something
}
}
I've tried a few different things to no avail. The error console is telling me that the input is undefined. For example, I started with:
var placeholder = form.input.placeholder;
and that didn't work either.
I then ran some alert tests and when I just alert the input I get NodeList
object, which I'm not sure how to deal with.
So I tried looping through the NodeList
and got some objects I'm unsure how to use.
var inputList = document.getElementsByName("Name");
var inputArray = [];
for (var i = 0; i < inputList.length; i++) {
inputArray[i] = inputList[i];
alert(inputArray[i]);
}
Alert gives [object HTMLInputElement]
Basically, I just want to test against the placeholder value in the form with the specific name attribute. Seems I've gone awry...
Thanks.
As @apsillers correctly stated, the elements re turned are in an array so I needed to capture the one I needed. I went with this:
placeholder = document.getElementsByName("Name")[0].placeholder;
and then was able to use this in the function as required.