Change link with onClick event - Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David Hirst
    New Member
    • Dec 2011
    • 6

    #1

    Change link with onClick event - Javascript

    Hi
    I am new here and my JavaScript knowledge is fairly limited. I am trying to find a way to change a link with an on click event. At the moment when you click on the link it changes the image and text stored in the imgflip div.

    I would also like to be able to change the link stored there as the idea is that the user can look at the image, read the text and then click the link. Or ideally the whole image/text area is a link and the user can click anywhere. Though having a link at the bottom would still be best and look better.


    Here is a screenshot of the website: http://www.imagehosting.nu/?v=javascriptproblem.jpg

    and here is the code:


    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Wordpress Websites</title>
    <link href="home2012 css.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
    		
    function changeImage(newimage) {
    		var changeimg = document.getElementById("imgchange");
    
    		changeimg.src = newimage; 
    }
    function changedescription(newtext) {
    		var description = document.getElementById("description");
    			
    		description.innerHTML = newtext;				
    }
    function changewebaddress(newtext) {
    		var webdescription = document.getElementById("webdescription");
    			
    		webdescription.innerHTML = newtext;				
    }
    function changeurlMIC(urlMIC) {
    		var changeURLMIC = document.getElementById("URL");
    			
    		changeURLMIC.innerHTML = urlMIC;				
    }
    </script>
    </head>
    
    <body>
    <div class="maincontainer">
    
    <div class="navshow" id="navshow">
        <ul class="navshow">
        <li class="liheadingshow">
        <h2>Wordpress Websites</h2>
        </li>
    	<li><a href="#" onclick="changeImage('imgs/sampleMIC.gif'), changedescription('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras dui justo, cursus quis varius mattis, convallis nec justo. Suspendisse potenti. Suspendisse quis mauris lorem. Maecenas at nunc tortor, nec laoreet velit.'), changewebaddress('Click here: www.meditateincopenhagen.org'), changeurlMIC('http://meditateincopenhagen.org/'); return false">Meditate in Copenhagen</a></li>
        
    	<li><a href="#" onclick="changeImage('imgs/sampleMIB.gif'), changedescription('In laoreet sem vel mi suscipit placerat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.'), changewebaddress('Click here: www.meditasjonibergen.no'); return false">Meditate in Bergen</a></li>
        
    	<li><a href="#" onclick="changeImage('imgs/sampleNDC2012.gif'), changedescription('Proin facilisis velit vitae augue semper vitae sodales odio malesuada. In a nisl sit amet dui placerat rutrum vel vel tellus. Phasellus leo velit, pharetra id scelerisque eget, volutpat eu ipsum. Pellentesque eget ante justo, vel dignissim odio. '), changewebaddress('Click here: www.kadampafestivals.org/scandinavia'); return false">2012 - Nordic Dharma Celebrations</a></li>
        
        <li><a href="webdesign.html">Back</a></li>
    </ul>
    
    </div> <!--End of navbar -->
    <div class="imgflipshow">ยจ
    
      <p><img src="imgs/webdesign.png" width="350" height="200" id="imgchange"/></p>
      <p class="description" id="description">Click on one of the site from the menu to learn more.</p>
      <a href="#" id="URL">	<p class="description" id="webdescription" ></p></a>
    </div>
    
    
    </div>
    <div class="legal">Site Created by David Hirst 2011 - 2012. The website is licened under Creative Commons Licence BY, NC, SA. For more infomation click here. The images used on this website are copyright of there respective creators. See source code for more infomation. </div>
    </body>
    </html>
    It’s a bit messy as I haven't removed my attempts. Although hope you can understand the end goal.
    Last edited by acoder; Dec 13 '11, 12:37 PM. Reason: Added [code] tags
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    javascript code for accessing href of link is
    Code:
    anchorobject.href
    so just do this
    Code:
    function onclick_button()
    {
     anchorobject.href='www.new-link.com'; 
    }

    Comment

    • David Hirst
      New Member
      • Dec 2011
      • 6

      #3
      Thanks for your response, could you elaborate a bit? I am having trouble making that command change my link. Can you perhaps create a more detailed example using my code?

      Thank you for your help :).

      Comment

      Working...