As you can see below I have a sample JSON array object. That's how I want the JSON structure to look like at the end. I want the object generated dynamically based on the anchor tags which are inside a list. As you can see, the title is the name (html()) of the anchor tag and URL is the href attribute. So on click of the button create JSON, a function will loop through all the list and its anchor tag and create the object like the sample below. My attempt did not go very far, I was trying to get the html of all anchor tag then get like a.html() for title and a..attr('href') for the link. How would i go about doing this ?
JSON ARRAY OBJECT SAMPLE
[
{
"id": "1",
"Title": "Google",
"URL": "https://www.google.com"
},
{
"id": "1",
"Title": "yahoo",
"URL": "https://www.microsoft.com"
}
]
HTML
function createJson() {
var linkjson=[];
var listItemsA = $(".addlink_dynlist > li").find('a');
listItemsA.each(function(a) {
console.log(a);
//linkjson.push({id:1,Title:a.html()})
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div class="input_fields_wrap" style="overflow-y: hidden;">
<div class="addlink_dynlist">
<li>
<a class="addlink-a" href="https://www.google.com/">google</a>
<span class="show_field" hidden="">
<i class="fa fa-times addlink_dynlistX remove_field hand-cursor "></i>|
<i class="fa fa-pencil-square-o addlink-edit-icon hand-cursor" aria-hidden="true">Edit</i>
</span>
</li>
</div>
<div class="addlink_dynlist">
<li>
<a class="addlink-a" href="https://www.yahoo.com/">yahoo</a>
<span class="show_field" hidden="">
<i class="fa fa-times addlink_dynlistX remove_field hand-cursor "></i>|
<i class="fa fa-pencil-square-o addlink-edit-icon hand-cursor" aria-hidden="true">Edit</i>
</span>
</li>
</div>
<div class="addlink_dynlist">
<li>
<a class="addlink-a" href="https://www.cnn.com/">cnn</a>
<span class="show_field" hidden="">
<i class="fa fa-times addlink_dynlistX remove_field hand-cursor "></i>|
<i class="fa fa-pencil-square-o addlink-edit-icon hand-cursor" aria-hidden="true">Edit</i>
</span>
</li>
</div>
<div class="addlink_dynlist">
<li>
<a class="addlink-a" href="http://www.foxnews.com/">fox</a>
<span class="show_field" hidden="">
<i class="fa fa-times addlink_dynlistX remove_field hand-cursor "></i>|
<i class="fa fa-pencil-square-o addlink-edit-icon hand-cursor" aria-hidden="true">Edit</i>
</span>
</li>
</div>
</div>
<button onclick="createJson()">Create json</button>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="./js/test.js"></script>
</body>
</html>