0

Please, code to change domain in href (html, JavaScript).

Exemple:

<a id="NewL" href="http://exemple.com/indice.html">Indice</a>

To:

<a id="NewL" href="http://exemple.net/indice.html">Indice</a>

Exemple code not working:

<script  type = "text/javascript" >
function replace() {
    var aEls = document.getElementById('NewL').getElementsByTagName('a');
    aEls.href = aEls.href.replace('http://exemple\.com', 'http://exemple\.net');
}
</script>

Thanks, @t.niese:

<a id="NewL" href="http://exemple.com/indice.html">Indice</a>

<script  type="text/javascript" >
function replace() {
    var aEl = document.getElementById('NewL');
    aEl.href = aEl.href.replace('http://exemple.com', 'http://exemple.net');
}
replace();
</script>

Please help me, not change in various ID in same page:

   <a id="NewL" href="http://exemple.com/indice.html">Indice</a>
   <a id="NewL" href="http://exemple.com/indice2.html">Indice 2</a>

    <script  type="text/javascript" >
    function replace() {
        var aEl = document.getElementById('NewL');
        aEl.href = aEl.href.replace('http://exemple.com', 'http://exemple.net');
    }
    replace();
    </script>

2 Answers 2

1

Element.getElementsByTagName():

[...]The subtree underneath the specified element is searched, excluding the element itself.[...]

so you search for the elements with the tag name a within your element with the id NewL

Because document.getElementById('NewL') is already your a element, you won't need the getElementsByTagName('a'), as of that you should only write:

 var aEl = document.getElementById('NewL');

Also your replace is wrong, you don't need to escape the . if you pass the search as string.

 aEl.href = aEl.href.replace('htp://exemple.com', 'htp://exemple.net');

Beside that Element.getElementsByTagName() returns a list of elements, so even if your search would have been correct, you would need to use a loop to iterate through that result.

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

4 Comments

@jmsmarcelo Here is a jsfiddle and it is working fine, the .com is replaced by the .net, so you most likely did something else wrong.
@jmsmarcelo Are there error messages in the console? Are you calling the replace method?
@t.niese, Please, And when have several tag it only works in the first.
@jmsmarcelo ids have to be unique so you can only have on element with id="NewL" on your page, without knowing how you can identify the links you want to change it is not possible to answer your question.
0

change your javascript to the following:

<script  type = "text/javascript" >
function replace() {
    var aEl = document.getElementById('NewL');
    aEl.href = aEl.href.replace('htp://exemple\.com', 'htp://exemple\.net');
}
</script>

You already selected the element by the getElementById no need to find the a class in it. Also you mispelled the variable which I changed to aEl

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.