Having ajax request like:
$.ajax({
type: 'POST',
url: 'mysite/action',
dataType: 'json',
data: postData,
success: function(response) {
console.log(response);
var input = $('<a class="btn btn-info" target='_blank' href="....response...." role="button">Link</a>');
input.appendTo($("body"));
}
});
I get a link in ajax response, how do I create a button having its href attribute equal to that ajax response?
You can also pass attributes in an object, which will take care of saniziting strings for you, such as escaping quotes:
$('<a/>', {
'class': 'btn btn-info',
href: response,
role: 'button',
text: 'Link'
}).appendTo('body');
In response to comment below, simply pass a similarly constructed span to append
. Compared to the example below, I've removed the text
property from the attributes object, and add the link text in a subsequent append
call, to insert the icon before the text, if this is what you want.
It could of course have been achieved by adding the span with a prepend
instead, if you want to keep the text property, but I think it makes more semantical sense to add the elements in the order you want them to appear.
$('<a/>', {
'class': 'btn btn-info',
href: response,
role: 'button'
}).append(
$('<span/>', { 'class': 'glyphicon glyphicon-download' })
).append(' Link')
.appendTo('body');
You can use Template Strings
Template literals are string literals allowing embedded expressions
var input = $(`<a class="btn btn-info" target='_blank' href="${response}" role="button">Link</a>`);
Or you can use the +
operator and break string into to parts.
var input = $('<a class="btn btn-info" target='_blank' href="'+15+'"role="button">Link</a>');
Try this solution:
$.ajax({
type: 'POST',
url: 'mysite/action',
dataType: 'json',
data: postData,
success: function(response)
{
console.log(response);
var input = $('<a class="btn btn-info" target='_blank' href="' + response + '" role="button">Link</a>');
input.appendTo($("body"));
}
});
I'm trying to loop through nested arrays to determine if an element in the array is either "open" or "senior": function openOrSenior(data) { for (let i = 0; i <= data.length; i++) { let ...
I'm trying to loop through nested arrays to determine if an element in the array is either "open" or "senior": function openOrSenior(data) { for (let i = 0; i <= data.length; i++) { let ...
I have this array air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { ...
I have this array air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { ...
I'm using angular-nvd3 directive for making a custom line chart display counting number of guest in specific period time range as follow : current Time - 2 --> current Time : will be display as ...
I'm using angular-nvd3 directive for making a custom line chart display counting number of guest in specific period time range as follow : current Time - 2 --> current Time : will be display as ...
how to make setTimeout() work as it looks like in for-loop? like this code function hello() { for (let index = 0; index < 3; index++) { setTimeout(function () { console....
how to make setTimeout() work as it looks like in for-loop? like this code function hello() { for (let index = 0; index < 3; index++) { setTimeout(function () { console....