Reputation: 1804
hi i have been trying to get the id of the current element that I am looping through but I am not sure why this doesnt work any help is greatly appreciated!
code:
var arr = document.getElementsByClassName("myclass");
for (i = 0; i < arr.length; i++) {
var key = "";
var value = "";
if (arr[i].nodeName == "INPUT" || arr[i].nodeName == "TEXTAREA") {
key = arr[i].name;
value = arr[i].value;
}
else if (arr[i].nodeName == "SELECT" && arr[i].attr('id') == "multiple") {
alert("worked");
key = arr[i].name;
value = "multi";
}
else if (arr[i].nodeName == "SELECT" && arr[i].attr('id') != "multiple") {
alert("ddint work");
key = arr[i].name;
value = "not multi";
}
}
I am not able to get the id from my arr[i] position even though it is an element.
Upvotes: 0
Views: 797
Reputation: 10206
var arr = document.getElementsByClassName("myclass");
for (i = 0; i < arr.length; i++) {
var key = "";
var value = "";
if (arr[i].nodeName == "INPUT" || arr[i].nodeName == "TEXTAREA") {
value = arr[i].value;
}
else if (arr[i].nodeName == "SELECT" && arr[i].id == "multiple") {
alert("worked");
value = "multi";
}
else if (arr[i].nodeName == "SELECT" && arr[i].id != "multiple") {
alert("ddint work");
value = "not multi";
}
key = arr[i].name;
}
.attr("id") is a jquery function. The javascript equivalent is .id
Also you can move the key variable assignment outside of the if statements because you assign it regardless of the if statements.
Upvotes: 1
Reputation: 95023
arr[i]
is an element, not a jQuery object. you can access the id
property directly with arr[i].id
Upvotes: 3