last day (17 days later) » 

8:50 PM
1
A: How to access the corresponding object's value according to the click in javascipt

Timothy KanskiYou'll need to write the results to an array, then rewrite the div contents. Use .html() instead of .append() https://jsfiddle.net/s02ch9mt/1/

 
BlueRaven gave me the best answer, so I took it, but still thanks for your help.
Your methode is good too, rewrite the html with the check, I need more time to compare BlueRaven's and your's.
I tried your code too, I write the same way as yours, but, when I click on 'apple', it shows 'apple x 1', click on apple again, it shows 'apple x 2' under 'apple x 1', and the third click on 'apple', it continuts to append 'apple x 3' under 2 and 1, then I click on 'kiwi', it shows 'kiwi x 1' under a new 'apple x 3', click 'kiwi' again, it continuts to append 'apple x 3' and 'kiwi x 2' under 'apple x 3' and 'kiwi x 1', seems it's doing append, but in your code, the string will overwrite and just add 1 to the nimber, du you have any idea why this happen?
I put my code based on yours in jsFiddle, almost the same code, but my code just don't works as yours, can't figure out.
 
You're one character away from the right answer. Change: myString += a.nameArray[i] + " x " + a.valueArray[i] + "<br/>"; to myString = a.nameArray[i] + " x " + a.valueArray[i] + "<br/>"; (Notice the = instead of +=). The reason is, we need to clear the string each time, or it writes the last iteration's data too.
 
Still not working. I changed the '+=' to '=', in this case, when I click on the second fruit, it will replace the first one I've already clicked, and if I click the first one again, it dose nothing. Did you find something different between your code and mine?
I just compare line by line, and I found it!!! It's the variable myString=" ", I saw you created it ouside the click, so I did not write it again inside the click before the 'for', and that is the problem, I put it inside the click, and it works as yours!!! I don't understand why, but with or without this variable in the right place, it's so important and different. Could you understand why? And thanks for you continuous help!
 
You are using the += operator to APPEND data to myString each time a link is clicked. If you do not use myString = " " to clear the myString variable of all previous data, then when a link is clicked the second time, the myString variable still contains the text "apple x 1". Then "apple x 2" gets APPENDED to it (because of the +=). That's causing the problem you noticed. What you need is to REPLACE the contents of myString instead of APPEND to it. You could use = instead of +=. Then you won't need to clear myString each time the function is called. Hope this helps!
 
Sorry for bothering you again, for the previous questiions, it's solved. I'm now coding based on your code, there are some new questions, I put my code on jsFiddle, now I want 2 more button, 'minus' and 'delete', 'minus' to minus one from the quantity, 'delete' to remove the transaction. So far, I made both of them but not working well, 'minus' works when there's only one fruit, once it's more than one, it works on everybody and it's a chaos, I know that I need a this somewhere but I don't know how to do. And the 'delete' is worse, can't solve it.
And for the 'minus' button, how to stop it's function when the quantity remains one? I tried a do...while but not working.
 
8:50 PM
I use a check for value > 1 in this fiddle: jsfiddle.net/uwqdgnjn
 
9:02 PM
I meant < 1
 
but I saw in your Fiddle is to check the value > 1, and it works
 
9:52 PM
Ooops! You're right. So is everything resolved now?
 
lool, nop, not yet
you get the fruit name by innerHTML from the button to show me an example
so I created an id to the minus botton to get the fruit name, I check it by an alert, it's good, I can get the fruit name according to the click
then I do the same code as you, but it doesn't work, the 'minus' button do nothing, I don't get it
maybe I need to redo the check line by line
 

  last day (17 days later) »