Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    href url based on the first-child of the list

    Hi!
    I need help with this...
    Lets say I have a code like this:

    Code:
    <a href="#" id="clickme">Click me</a>

    And I want to change the above code href attributes with the href of....


    <ul id="list">
    <li><a href="http://www.google.com">ABC</a></li>
    <---- ....this element
    <li><a href="http://www.yahoo.com">DBF</a></li>
    <li><a href="http://www.javascriptkit.com">GHI</a></li>
    </ul>


    So, can you guys help me?
    Really need this thing to work...
    Last edited by alvin_bilian; 12-19-2006 at 01:02 PM.

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I really am not sure what you are asking but here is some interesting code that might be similar to your question

    Code:
    <html>
      <head>
        <title>Change HREF</title>
        <script type="text/javascript">
          function msg(s) {
            var adiv = document.getElementById("adiv");
            adiv.innerHTML = s;
          }
    
          function changeHref(oThis) {
            document.getElementById("clickme").href = oThis.href;
            msg("Changed link to " + oThis.href);
            return(false);
          }
        </script>
      </head>
      <body>
        <div>
          <a href="#" id="clickme">Click me</a>
    
          <ul id="list">
            <li><a href="http://www.google.com" onclick="return changeHref(this);">ABC</a></li> <!-- ....this element  -->
            <li><a href="http://www.yahoo.com" onclick="return changeHref(this);">DBF</a></li>
            <li><a href="http://www.javascriptkit.com" onclick="return changeHref(this);">GHI</a></li>
          </ul>
          <div id="adiv"> </div>
    
        </div>
      </body>
    </html>
    david_kw

  • #3
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    alvin_bilian:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	function init(){
    
    		var firstItem = document.getElementById('list').getElementsByTagName('li')[0].firstChild.href;
    		document.getElementById('clickme').href = firstItem;
    	}
    
    	onload=init;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	
    </style>
    </head>
    	<body>
    		<a href="#" id="clickme">Click me</a>
    		<ul id="list">
    			<li><a href="http://www.google.com">ABC</a></li> 
    			<li><a href="http://www.yahoo.com">DBF</a></li>
    			<li><a href="http://www.javascriptkit.com">GHI</a></li>
    		</ul>
    	</body>
    </html>

  • #4
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All Right thanks guys for your help!!
    Its working perfectly!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •