0

I'm using this Javascript code in an attempt to replace the links on a page with a message that says "DOWNLOAD", with a hyperlink that leads to my registration page.

The problem is the "DOWNLOAD" text is not replacing the original link text. The original link is displayed. It does lead to the registration page, but again, the original link on the page is still visible as text.

Any ideas?

<script>
    function replaceLinks() {
        var links = document.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++) {
            links[i].innerHtml = 'DOWNLOAD' + 
                    '<a href="register.php">register here</a>.';
            links[i].href = 'register.php';
        }
    }
</script>
2
  • You're putting a link inside another link. I don't think that will work. Commented Apr 10, 2014 at 3:54
  • It's innerHTML, not innerHtml. Case is significant. Commented Apr 10, 2014 at 3:57

3 Answers 3

1

It should be:

function replaceLinks() {
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        links[i].innerHTML = 'DOWNLOAD register here.';
        links[i].href = 'register.php';
    }
}

The property is innerHTML, the last part is all uppercase. And you don't need to nest another link inside the link.

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

Comments

1

It looks like you have a capitalization issue - it should be innerHTML. You can also remove other parts of your code:

function replaceLinks() {
var links = document.getElementsByTagName('a');

for (var i = 0; i < links.length; i++) {
    console.log(links);
    links[i].innerHTML = 'DOWNLOAD';
    links[i].href = 'register.php';
    }
};

Comments

0

In jQuery way,

function replaceLinks() {
    var links = $('a');
    for (var i = 0; i < links.length; i++) {
        links[i].html('DOWNLOAD register here.');
        links[i].attr('href') = 'register.php';
    }
}

Comments

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.