1

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);
    }
}
6
  • Can you post your HTML where the 'link' needs to be extracted from? Commented Mar 11, 2013 at 14:47
  • If you know how to use querySelectorAll, what is the problem? oO Commented Mar 11, 2013 at 14:48
  • I just did few problems with formatting. Is marking links using class tag smart thing to do? Commented Mar 11, 2013 at 14:48
  • I do not know javascript syntax at all, so it would probably take a whole day for me to do this, and i guess this is a simple regex. Isn't it? Commented Mar 11, 2013 at 14:49
  • @Zox marking nodes with classes is super smart things to do! But better give some more meaningful names to your classes. Instead of 1 it could be external-link, etc. Commented Mar 11, 2013 at 14:49

3 Answers 3

1

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>

http://jsfiddle.net/Tqv76/2/

Sign up to request clarification or add additional context in comments.

3 Comments

Work very well. Thanks for tip that classes cannot start with numbers.
You are welcome. I would appreciate if you accepted the answer (and the previous).
I didn't know that there is this option. How would i combine functions changespan1 and another one lets say changespan2?
0
document.getElementsByTagName("a")[0].getAttribute("href");

3 Comments

So I say a.href = document.getElementsByTagName("a")[0].getAttribute("href"); ?
I'd think so. Give it a go.
The [0] implies the first anchor in the page, so I'm not sure if that's appropriate in your case.
0
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);
    }
}

4 Comments

1) there is no jQuery. 2) Never do things like that (query DOM in loops).
All of us know that there is jQuery. Then why are you pretending like it is not existing ?
How do you know that OP has jQuery?
I have but as @dfsq said class name cannot start with a number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.