javascript - how to append a new hyperlink with a new id each time in a loop? -
it's second day on project :\
i'm trying create : creating new <a>
element new href
, id
attributes in for
loop can each output of api link.
this js code
var one; var two; var hoba; $(document).ready(function() { $("#inp").keyup(function() { hoba = $(this).val(); }); $("#but").on("click", function() { var app = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=revisions&list=search&titles=main+page&rvprop=content&origin=*&srsearch=" + hoba; $.getjson(app, function(data) { (i = 0; < data.query.search.length; i++) { console.log(app); 1 = $("<a></a>").text(data.query.search[i].title); //var _href = $("a").attr("href"); $("a").attr("href", 'https://www.wikipedia.org/wiki/' + data.query.search[i].title); $("a").attr("id", data.query.search[i].title); 2 = document.createelement("p"); two.innerhtml = data.query.search[i].snippet; $("body").append(one, two); } }); }); });
use same object set attributes
one = $("<a></a>"); one.text(data.query.search[i].title); one.attr("href", 'https://www.wikipedia.org/wiki/' + data.query.search[i].title); one.attr("id", data.query.search[i].title);
use jquery( html, attributes )
create html element.
var anchor = $("<a></a>", { "text": data.query.search[i].title, "href": 'https://www.wikipedia.org/wiki/' + data.query.search[i].title, "id": data.query.search[i].title }); $("body").append(anchor);
$(document).ready(function() { $("#inp").keyup(function() { hoba = $(this).val(); }); $("#but").on("click", function() { var app = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=revisions&list=search&titles=main+page&rvprop=content&origin=*&srsearch=" + hoba; $.getjson(app, function(data) { (i = 0; < data.query.search.length; i++) { var anchor = $("<a></a>", { "text": data.query.search[i].title, "href": 'https://www.wikipedia.org/wiki/' + data.query.search[i].title, "id": data.query.search[i].title }); var p = $("<p></p>", { "html": data.query.search[i].snippet }); $("body").append(anchor); $("body").append(p); } }); }); });
Comments
Post a Comment