0

I want to add a hyperlink on the ribbon. I was able to add the hyperlink to the existing div. The Hyperlinks are getting increased by one for every action i do on the page. How i can make it(hyperlink) restrict to one?

I am using the following code:

var mydiv = document.getElementById("myDiv"); 
var aTag = document.createElement('a');
aTag.setAttribute('href',"yourlink.htm");
aTag.innerHTML = "link text";
mydiv.appendChild(aTag);

Please suggest me.

5
  • 2
    Where's the cooooooddddeeeee? Commented Aug 9, 2012 at 10:00
  • 2
    Do you have any demo that demonstrates what's happening? Commented Aug 9, 2012 at 10:01
  • I am following the below code var mydiv = document.getElementById("myDiv"); var aTag = document.createElement('a'); aTag.setAttribute('href',"yourlink.htm"); aTag.innerHTML = "link text"; mydiv.appendChild(aTag); Commented Aug 9, 2012 at 10:19
  • 3
    Edit question and put code there. Commented Aug 9, 2012 at 10:25
  • from now on...you should keep your posts like this as the new edit made to it...instead of posting code into comments..This improves readability of your question and make it much understandable for others... Commented Aug 9, 2012 at 11:12

1 Answer 1

1
var aTag = document.createElement('a');

Creates a new anchor element everytime it's called. Without more context it's hard to see why and how often this code is called.

What we do know, is every time it's called, you're appending it to the div, which is why you have multiple links.

mydiv.appendChild(aTag);
Sign up to request clarification or add additional context in comments.

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.