0

I have a difficulty with mapping my my Json data. I would like to add data attr to each div with .name class. So as the result is like that:

<div class="name" data-key="sth"> sty</div>

Key can be got like that: ['streme'].key

here is my buggy JS:

function getExistingLinks() {
          $.post( "http://0.0.0.0:9292/api/links", function( data ) {
            var names = data.map(function (i) {
            return i['link'].name
            });
            var keys = data.map(function (i) {
            return i['link'].key
            });
            var container = document.querySelector(".link-names");
                names.forEach(function(name) {
                    var div = document.createElement('div');
                    div.innerHTML = name;
                    $('div').addClass("name");
                    // $('div').each( function(index) {
                           $('div')[index].data("key") = keys[index];
                       }
                    container.appendChild(div);
                });
       });  
      return false;   
    }
2
  • what does console.log( keys ); show..? Commented Oct 22, 2013 at 4:29
  • is hows 33 times Array[33] and in each of these 33 keys like that: 0: "p0E2bVUfq3zIRuUO9zY8X5ZbwAM" Commented Oct 22, 2013 at 4:33

4 Answers 4

1
names.forEach(function(name,index) {
  var div = document.createElement('div');
  div.innerHTML = name;
  $(div).addClass("name");
  $(div).data("key") = keys[index];
});

You need to remove the quotes in the $() selector!

Sign up to request clarification or add additional context in comments.

4 Comments

Uncaught ReferenceError: i is not defined
@lipenco you need a loop around the index! The point is to remove the quotes and use $(div) instead of $('div')
hmm.. but $(div).each(function( index ) { $(div).data("key") = keys[index]; }); doesn't work: invalid left side of assignment
I have updated my answer. The issue in your original code is that $('div') gets all the div elements in the document, while what you really wanted was $(div) to grab the div you just created.
0

As per your comment, may be try doing like:

var i = 0;
names.forEach(function(name) {
    var div = document.createElement('div');
    div.innerHTML = name;
    $('div').addClass("name");
    $('div').data("key", keys[i]);
    container.appendChild(div);
    i++;
});

2 Comments

$(div).data("key") = keys[i]; - I have an error: invalid left side of assignment
@lipenco see my updated answer.. do this:: $('div').data("key", keys[i]); instead
0

the preferred method is to only add to the DOM once, as adding to the DOM will cause a redraw on each.

psuedo code as not sure what name represents in your innerHTML:

var divs = []; for (var i, len = names.length; i < len; i++) { divs.push($(''+name+'').data("key", keys[i])); }

$container.append(divs);

http://codepen.io/jsdev/pen/2866265243563efd79cf05a5b12202b3

Comments

0

try something like this

 $('.name').data('key') //will give you sth

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.