Set innerHTML of anchor in JavaScript - Stack Overflow

Here is my codevar test = '${URL}'${URL} is an anchor tag containing the follow <a href=&q

Here is my code

var test = '${URL}'

${URL} is an anchor tag containing the follow <a href="google">LINK</a>

I want to change this LINK to TEST. Is it possible to do that? I couldn't find any method to just change the innerHTML of the anchor tag.

Here is my code

var test = '${URL}'

${URL} is an anchor tag containing the follow <a href="google.">LINK</a>

I want to change this LINK to TEST. Is it possible to do that? I couldn't find any method to just change the innerHTML of the anchor tag.

Share Improve this question edited Jan 25, 2014 at 2:25 Antony 15.1k10 gold badges47 silver badges76 bronze badges asked Jan 25, 2014 at 2:17 CamelCaseCamelCase 2333 gold badges6 silver badges17 bronze badges 2
  • 1 '${URL}' is not JavaScript nor HTML. I'm not sure if this is any language related to JS, but if it is, please include that in the tags to your question. Also, try doing test.src = 'http://google.' to change your target url to http://google.. Is that what you're looking for? – Joeytje50 Commented Jan 25, 2014 at 2:21
  • looks like some template replacement code – Patrick Evans Commented Jan 25, 2014 at 2:27
Add a ment  | 

2 Answers 2

Reset to default 4

First give an id to the html link. Then you can access it and update the text by using innerHTML property as below example.

<html>
<head>
</head>
<body>
<a href="google." id='link'>LINK</a>

<script>
    document.getElementById('link').innerHTML="Test";
</script>
</body>
</html>
  • Reference: http://www.w3schools./jsref/prop_html_innerhtml.asp

The variable 'test' does not hold a DOM object. All that is to it at that point is a String that happens to look like html.

If you did this with the test variable.

var div = document.createElement('div');
div.innerHTML += test;

div.getElementsByTagName("a")[0].innerHTML = 'test';

You could do it. But at that current stage in your code, the variable test is no more that a String.

If you could create a better picture of the environment and what you want to happen we could help you a lot better.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744235023a4564431.html

相关推荐

  • Set innerHTML of anchor in JavaScript - Stack Overflow

    Here is my codevar test = '${URL}'${URL} is an anchor tag containing the follow <a href=&q

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信