0

I need some pointer here. I am trying to set the value of multiple input text within a class by its index value. The extra validate class is just for inline form validation and I think it is not the problem.

I was able to access multiple check boxes within a class using the same method but I can't figure this one out. Can someone please give me a hint as to what I did wrong? Thank you.

Script

//--(4) Load Delivery Cost Input Box
var tmpstr = $('#editDeliveryCost').html();
if (tmpstr != ""){
   var tmparray = tmpstr.split(',');
   for (i = 0; i < tmparray.length; ++i) {
      $('.typeDeliveryCost:eq('+(tmparray[i]-1)+')').val(tmparray[i]);
   }

HTML

<input type="text" name='cost[]' id='inp01' class="validate[required,custom[price]] typeDeliveryCost" />
<input type="text" name='cost[]' id='inp02' class="validate[required,custom[price]] typeDeliveryCost" />
<input type="text" name='cost[]' id='inp03' class="validate[required,custom[price]] typeDeliveryCost" />
<input type="text" name='cost[]' id='inp04' class="validate[required,custom[price]] typeDeliveryCost" />
4
  • What output are you getting now? Do you get any errors? Can you post a jsfiddle.net demonstrating the problem? Commented May 17, 2011 at 3:22
  • What is the value of tmpstr? How about a demo? Side note, you should really be more consistent with using single/double quotes in both markup (delimiting attributes) and in JS (delimiting strings). Commented May 17, 2011 at 3:24
  • I have checked that the array are containing the right data I wanted so there are no issue with tmpstr or tmparray. Commented May 17, 2011 at 3:26
  • @mellamokb : I am getting nothing in the input boxes. Commented May 17, 2011 at 3:27

2 Answers 2

1

change this

$('.typeDeliveryCost:eq('+(tmparray[i]-1)+')').val(tmparray[i]);

to

$('.typeDeliveryCost:eq(' + i + ')').val(tmparray[i]);
Sign up to request clarification or add additional context in comments.

1 Comment

You are right, I made a mistake on assigning the index to the array value, silly me. Thanks.
0

Shouldnt

$('.typeDeliveryCost:eq('+(tmparray[i]-1)+')').val(tmparray[i]);

be

$('.typeDeliveryCost:eq(' + i + ')').val(tmparray[i]);

:eq(index) takes a zero-based index (eq-selector)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.