inktherapy Posted November 14, 2008 Share Posted November 14, 2008 Hi,I am writing a code for my news article (just one of my exercises in Javascript). I need to have a link that will display "read more" in html e.g. <div><a href="www.w3shools.com">read more</a></div> and then I want to replace the value "read more" to its source link which is "www.w3schools.com" by triggering a button, this will now display <div><a href="www.w3schools.com">www.w3schools.com</a></div> in html. I have a sample code below(please check), but the problem is, if I have many articles that I want to process the same result as I mentioned above, how will I do it? What I am thinking now is writing the same pattern of codes but this will be much harder than manually copy and paste the source link to replace the read more.Help! Thanks. <html><head><script type="text/javascript">function getLink() {x = document.getElementsByTagName('a');document.getElementById('one').innerHTML=x[0];}</script></head><body><div>Man Killed by a killer mouse!</div><div id="one"><a href="http://www.w3schools.com" name="one">read more</a></div><button onclick="getLink()">Get now!</button></body></html> Link to comment Share on other sites More sharing options...
jeffman Posted November 14, 2008 Share Posted November 14, 2008 You were so close! function getLink () { links = document.getElementsByTagName("a"); for (var i = 0; links[i]; i++) { links[i].innerHTML = links[i].href; } } Link to comment Share on other sites More sharing options...
inktherapy Posted November 15, 2008 Author Share Posted November 15, 2008 Hi,This is great, wow! I really need to focus. Thanks Deirdre's Dad. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.