2

I have json array named JsonTempArray. And I have mappingId and Name as two fields.enter image description here

When clicking Male or Female It automatically creates 5 mappingids(1 to 5) and Name field is Empty. Like :-

JsonTempArray[length]=
 {
   mappingid: Number,//Number has 1 to 5
   Name:""
 }

And I have bellow text boxes for each 5 people to fill their name.

enter image description here

I have below piece code to update Name field

for(var len=0;len<JsonTempArray.length;len++)
     {
       if (JsonTempArray[len].Mappingid= mapid ) {
           JsonTempArray[len].Name= document.getElementById('txtName'+len).value;
            }
      }

I will pass Particular mappingid while clicking textbox to this.

Eg: Input is:

1 John
2 Jack
3 Kin
4 Fin
5 Hol

But After updating JsonTempArray has

5 Hol
5 Hol
5 Hol
5 Hol
5 Hol

Please help me to find how to update each value in loop.

Thank you

5
  • Have you checked what is the value of mapid ? Commented Apr 20, 2015 at 6:19
  • if i will type value for 1st text box , mapid is 1 ,if 2nd mapid is 2 and so on. Commented Apr 20, 2015 at 6:25
  • If possible, can you create a fiddle? Commented Apr 20, 2015 at 6:28
  • use == in IF condition instead of = Commented Apr 20, 2015 at 6:58
  • i tried with ==. But its javascript so single = is working. Commented Apr 20, 2015 at 7:05

2 Answers 2

1

The issues is here instead of checking you where actually assigning:

if (JsonTempArray[len].Mappingid= mapid ) {

It should be:

if (JsonTempArray[len].Mappingid == mapid ) {
Sign up to request clarification or add additional context in comments.

Comments

1

I found solution. I should not use for loop in my case.

JsonTempArray[id].Name= document.getElementById('txtName'+id);//id is 1/2/3/4/5(mappingid)

Thank you for helping.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.