Reputation: 303
I need to extract URL link
from html.
<a rel="nofollow" href="link" class="1">
There is only one such link on the whole page.
Then I need to add use it as a.href
in this function:
function changespan() {
var spans = document.querySelectorAll('span.image');
for (var i = spans.length; i--; ) {
var a = document.createElement('a');
a.href = "http://domain.com";
spans[i].appendChild(a).appendChild(a.previousSibling);
}
}
Upvotes: 1
Views: 2658
Reputation: 193271
Try this:
function changespan() {
var spans = document.querySelectorAll('span.image'),
href = document.querySelector('.nofollow-link').href;
for (var i = spans.length; i--; ) {
var a = document.createElement('a');
a.href = href;
spans[i].appendChild(a).appendChild(a.previousSibling);
}
}
but change you link class to something else then 1
it can't start with number:
<a rel="nofollow" href="link" class="nofollow-link">Link</a>
Upvotes: 1
Reputation: 762
function changespan() {
var spans = document.querySelectorAll('span.image');
for (var i = spans.length; i--; ) {
var a = document.createElement('a');
a.href = $(".1").attr('href');
spans[i].appendChild(a).appendChild(a.previousSibling);
}
}
Upvotes: 0