In the process of learning object literals and was experimenting with the following code. I'm trying to get the values of inputs based on the css class (.one
,.two
). I have created the two 'variables',oneInput
and twoInput
to get the elements that match each class. Then I've assigned the two variables to the array, which I'm planning to access within getInputValues
function but the array is returning undefiend. I did read a few threads about object literals and the use of 'this' but not sure how to overcome this situation.
However, if I create another function inside the object and return the same array it works but I'd like to know if I can achieve the same with this current implementation.
var myObj = {
oneInput: $('.one'),
twoInput: $('.two'),
arr: [this.oneInput, this.twoInput],
getInputValues: function() {
console.log(this.arr);
},
eventBind: function() {
this.oneInput.on('blur', this.getInputValues.bind(this));
}
}
myObj.eventBind();
.one,
.two {
display: block;
margin-bottom: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="one" />
<input type="text" class="one" />
<input type="text" class="one" />
<input type="text" class="one" />
<hr />
<input type="text" class="two" />
<input type="text" class="two" />
<input type="text" class="two" />
<input type="text" class="two" />
Any help, adivce is massively appreciated.