1

I have this json

"urls": {
      "title1": {
        "url": "someurl1"
      },
      "title12": {
        "url": "someurl2"
      }
    }

I want get title and value of url out in an element for each. Something like this

$.each(value.urls, function (key, value) {
    $('.log-list-js').append('<a href="'+ title +'">'+ someurl +'</a>');
});

Hope it makes sense.

Can you help me ?

3 Answers 3

2

Try this

   var values = {
        "urls": {
            "title1": {
                "url": "someurl1"
            },
            "title12": {
                "url": "someurl2"
            }
        }
    }
    for (var key in values.urls) {
        $('.log-list-js').append('<a href="' + values.urls[key].url + '">' + values.urls[key].url + '</a>');
    }

DEMO

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

2 Comments

Thanks, but what about the title? That should be the text of each element?
I found out 'key' : )
0
$('.log-list-js').append('<a href="' + value.urls[key].url + '">' + value.urls[key].url + '</a>');

This is format

Comments

0

Your json should be converted to object first, use JSON.parse method if you are having a json string. object should something like this

urls = {
      "title1": {
      "url": "someurl1"
      },
      "title12": {
        "url": "someurl2"
      }
    };

then your method is correct with some corrections to the code

$.each(urls, function(index, value){
    $('.log-list-js').append('<a href="'+ index +'">'+ value.url +'</a>');
});

here is the demo on jsfiddle

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.