File json producto.json
{
"produto":[
{"categoria": "#home", "nome": "home"},
{"categoria": "#fotos", "nome": "fotos"},
{"categoria": "#contato", "nome": "contato"},
{"categoria": "#perfil", "nome": "perfil"},
{"categoria": "#painel", "nome": "painel"}
]
}
test.js file
$(function(){
var nome_a = ['home', 'fotos', 'contato', 'perfil', 'painel'];
var n = '';
var el = $('ul');
for(var i = 0; i < nome_a.length; i ++){
console.log('<li><a>'+nome_a[i]+'</a></li>');
const y = '<li><a>'+nome_a[i]+'</a></li>';
el.append(y);
}
//configura atribute href no elemento <a>
$.ajax({
url: 'produto.json',
type: 'get',
dataType: 'json'
}).done(function(data){
$('a').each(function(){
for(var i = 0; i < data.produto.length; i++){
//console.log(data.produto[i]);
// n += event.type;
n += data.produto[i]["categoria"];
}
$(this).attr('href', n);
})
}).fail(function(){
console.log("Erro no carregamento das categorias");
})
})
What I'm doing is not a project, it's just a challenge.
What I want is to get the product category and add as a link in the href of the <a>
element by making ajax () request.
The first line of code in the test.js file is creating the <li><a>
tags with the category names of var nome_a
, and creating it with a for()
loop.
The second part now is to get the categories with ajax () and add in the href attribute, but when I do this is to add all the categories in a single <a>
tag. Result example: <a href="#home#fotos#contato...">home</a>
What I want is to make each category stay in every <a href="[categoria aqui]">
tag within href. Anyone there able to get this result?