I am trying to make my javascript script more dynamic but I am not really use to work with it, so I need help figuring out is it possible to add counter to the property names?
So I would like this code:
var title1 = document.getElementsByClassName('requestRightsTitle1')[0].value;
var email1 = document.getElementsByClassName('requestRightsEmail1')[0].value;
var title2 = document.getElementsByClassName('requestRightsTitle2')[0].value;
var email2 = document.getElementsByClassName('requestRightsEmail2')[0].value;
var title3 = document.getElementsByClassName('requestRightsTitle3')[0].value;
var email3 = document.getElementsByClassName('requestRightsEmail3')[0].value;
var title4 = document.getElementsByClassName('requestRightsTitle4')[0].value;
var email4 = document.getElementsByClassName('requestRightsEmail4')[0].value;
to do something like:
var inputFields = [];
for (i = 1; i <= 5; i++) {
inputFields.push({
title: document.getElementsByClassName('requestRightsTitle'+i)[0].value,
email: document.getElementsByClassName('requestRightsEmail'+i)[0].value
})
}
But i need this title and email to be title1, email1, title2, email2...
I tried:
for (i = 1; i <= 5; i++) {
inputFields.push({
'title'+i: document.getElementsByClassName('requestRightsTitle'+i)[0].value,
'email'+i: document.getElementsByClassName('requestRightsEmail'+i)[0].value
})
}
and similar things like that, but no success. If there is a way to achieve that, please share your solution. Thanks in advance.
inputFields[i].title? replace i for the index of the one you want to view