0

Is it possible to do this:

<a class="a-link" href="/targetlink" target="_blank" rel="nofollow">Download</a>
<a class="a-link" href="/targetlink" target="_blank" rel="nofollow">Alternative</a>

replaced automatically using JavaScript when the page has finished loading

<input type="button" class="a-link" onClick="parent.open('/targetlink')" formtarget="_blank">Download</input>
<input type="button" class="a-link" onClick="parent.open('/targetlink')" formtarget="_blank">Alternative</input>

2 Answers 2

1

When the document loads, loop over the targets, create a new element for each one found and replace the initial element with the new one:

window.addEventListener('load', function() {
    document.getElementsByClassName('a-link').map(function(elem){
        var newElem = document.createElement('input');
        newElem.type = 'button';
        newElem.classList = item.classList;
        newElem.target    = item.target;
        newElem.addEventListener('click', function(){ /* define click event here */ })
    });

    elem.parentElement.replaceChild(newElem, elem);
});

It can be made more robust, but for the scope of your question, it should provide an adequate answer.

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

Comments

1

You can do that by removing the existing tag and creating another tag with the same attributes.

Or you can simply do this with replaceWith().

$('#target').replaceWith('<newTag>' + $('target').html() +'</newTag>')

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.