Object property lost in loop

Mike Spider

I'm working with the local storage for the first time and going through some JS tutorials online as I'm quite rusty. Making a dictionary where you can add words in 2 idioms then it displays. Everything is working fine with the exception of the second item in the object "dict", is not being shown although it's in the object. it shows in the console output. Might be the indexing in the loop. Cannot see it though. Any light shed will be appreciated.

function getDictionary(){
    var dict = {},
        dictStr = localStorage.getItem('dictionary');

        if(dictStr !== null){
            dict = JSON.parse(dictStr);
    }


    return dict;

}


function show(){
    var html = "";
    var resultOutput = document.getElementById("resultOutput");
    var dict = getDictionary();
    for(var i = 0; i < Object.keys(dict).length; i++){
        key = Object.keys(dict)[i];
        html += "<p class='normal-paragraph'>The english word <span style='color: green'> " + key + 
            " </span>in portuguese is <span style='color: green'>  " + dict[key]+
            "</span> <button class='removeBtn' id='"+ i +"' >x</button></p>";

        resultOutput.innerHTML = html;


        var buttons = document.getElementsByClassName('removeBtn');
        for(var i = 0; i < buttons.length; i++){
            buttons[i].addEventListener('click',remove);
    }



}

    console.log("");
    console.log("All words: "+ JSON.stringify(dict));


}

HTML ...

<p id="sayHello"></p>

<fieldset id="translateField">
    <legend> Translate english to Portuguese</legend>
            <input type="text" id="newWordEng" name="newWordEng" value="" placeholder=" write the english word" >
            <input type="text" id="newWordPt" name="newWordPt" value="" placeholder=" write the portuguese word">
            <input type="button" id="addWordBtn" name="addWordBtn" value="Add New Word">
        </form>

</fieldset>

<div id="resultOutput">
</div>


<script src="src/translate.js"></script>
</body>
Jonathan Sterling

I think it's because you're using the variable "i" twice. In the inner for loop, try using "j" instead:

for(var j = 0; j < buttons.length; j++){
        buttons[j].addEventListener('click',remove);
}

Edit: In fact, you can probably remove the inner loop altogether, and just do:

function show(){
    var html = "";
    var resultOutput = document.getElementById("resultOutput");
    var dict = getDictionary();
    for(var i = 0; i < Object.keys(dict).length; i++){
        key = Object.keys(dict)[i];
        html += "<p class='normal-paragraph'>The english word <span style='color: green'> " + key + 
                " </span>in portuguese is <span style='color: green'>  " + dict[key]+
                "</span> <button class='removeBtn' id='"+ i +"' >x</button></p>";

        resultOutput.innerHTML = html;

        var buttons = document.getElementsByClassName('removeBtn');
        buttons[i].addEventListener('click',remove);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Object property reassignment in a loop

From Dev

Trying to get property of non-object.. I'm lost

From Dev

Discord.js .cleanContent property lost on Object.keys(), and not loggable

From Dev

create object property replacing every object property inside for loop

From Dev

Javascript - Loop through object with array property

From Dev

How to push an Object property into an Array in a for loop?

From Dev

loop through nested object and change property

From Dev

dynamically call an object property in a v-for loop

From Dev

JavaScript partial match object property in loop?

From Dev

Update Javascript object property inside for loop

From Dev

JS - Loop over array of object and output the property

From Dev

Error object property can not be iterated by for loop

From Dev

Loop to update a property in array object in Firebase?

From Dev

assign multiple values to object property with for loop

From Dev

Object does not support property or method ForEach Loop

From Dev

Change property of an object in a foreach loop php

From Dev

Appending property to stdClass Object in PHP while in loop

From Dev

calling function stored as a property in an object in a for loop

From Dev

Rename PHP Object property in foreach loop

From Dev

lost in loop foreach

From Dev

Use parent object property inside helper object loop Jsrender

From Dev

Loop Through Object Searching For Property And Modify Original Object on Success

From Dev

Access property of object nested in array nested in object with a for loop Mongoose

From Dev

Object properties are lost

From Dev

SVN migration lost lock property

From Dev

Mongoose save loop lost iterator

From Dev

Function reference lost outside loop

From Dev

The parameter lost value after a loop

From Dev

Difference between Object property accessing in Javascript using key or loop

Related Related

  1. 1

    Object property reassignment in a loop

  2. 2

    Trying to get property of non-object.. I'm lost

  3. 3

    Discord.js .cleanContent property lost on Object.keys(), and not loggable

  4. 4

    create object property replacing every object property inside for loop

  5. 5

    Javascript - Loop through object with array property

  6. 6

    How to push an Object property into an Array in a for loop?

  7. 7

    loop through nested object and change property

  8. 8

    dynamically call an object property in a v-for loop

  9. 9

    JavaScript partial match object property in loop?

  10. 10

    Update Javascript object property inside for loop

  11. 11

    JS - Loop over array of object and output the property

  12. 12

    Error object property can not be iterated by for loop

  13. 13

    Loop to update a property in array object in Firebase?

  14. 14

    assign multiple values to object property with for loop

  15. 15

    Object does not support property or method ForEach Loop

  16. 16

    Change property of an object in a foreach loop php

  17. 17

    Appending property to stdClass Object in PHP while in loop

  18. 18

    calling function stored as a property in an object in a for loop

  19. 19

    Rename PHP Object property in foreach loop

  20. 20

    lost in loop foreach

  21. 21

    Use parent object property inside helper object loop Jsrender

  22. 22

    Loop Through Object Searching For Property And Modify Original Object on Success

  23. 23

    Access property of object nested in array nested in object with a for loop Mongoose

  24. 24

    Object properties are lost

  25. 25

    SVN migration lost lock property

  26. 26

    Mongoose save loop lost iterator

  27. 27

    Function reference lost outside loop

  28. 28

    The parameter lost value after a loop

  29. 29

    Difference between Object property accessing in Javascript using key or loop

HotTag

Archive