Question

I hve this HTML:

<input type="text" id="S1" />
<input type="text" id="S2" />
<input type="text" id="S3" />
<input type="text" id="S4" /> 
<input type="text" id="S5" />

How can I achieve this pseudo-code:

var qData{};
for (i = 1; i == 5; i++) {
        qData.qBidS + i = $("#S" + i).val;
    }

At the end I want is a set of 5 qData: qData.S1, qData.S2, qData.S3, qData.S4, qData.S5

Was it helpful?

Solution

Use

var qData = {};
for(i = 1; i <= 5; i++) {
    qData["qBidS" + i] = $("#S" + i).val();
}

Problems:

  • Use .val(), there is no value property of jQuery object.
  • Use Bracket Notation and use string as per your requirement.
  • Condition should be i <= 5 not i == 5

OTHER TIPS

Demo Fiddle

jQuery

$('#textBoxCover input').each(function () {
     qData.push($(this).val());
});  

Hope it helps..!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top