views:

1311

answers:

4

hi,

I have below html:

<div class="threeimages" id="txtCss">  
<a>   
       <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
</a>        
    <div class="text" id="txtLink">            
     <h2>
      <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
     </h2>            
     <p>Land of the sunshine!</p>        
    </div>
</div>

Now if you see there is href in div ID "txtLink" i.e. Australia

I want this at the runtime of page the same href values get copied into above tag of div ID "txtCss", I mean when my page gets displayed my html will be as below:

<div class="threeimages" id="txtCss">  
<a href="/partnerzone/downloadarea/school-information/australia/index.aspx">   
       <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
</a>        
    <div class="text" id="txtLink">            
     <h2>
      <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
     </h2>            
     <p>Land of the sunshine!</p>        
    </div>
</div>

please suggest some code for above problem

+6  A: 

Go like this: Create a js file with something like this:

$(document).ready(function() {
    $('.threeimages').each(function(){
 $(this).DupeHref();
    });
});

jQuery.fn.DupeHref =  function(){  
    var target = $(this).find(".text h2 a").attr("href");
    $(this).find("a").attr("href", target);
}

Reference both the jquery and this javascript file in your html. Something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Test</title>   
 </head>
 <body>
  <div class="threeimages">  
   <a>   
       <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
   </a>        
   <div class="text">            
       <h2>
           <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
       </h2>            
       <p>Land of the sunshine!</p>        
   </div>
  </div>
  <div class="threeimages">  
                  <a>   
       <img alt="Belgium" src="/Images/Services%20button_tcm7-9689.gif"/>
   </a>        
   <div class="text">            
    <h2>
      <a href="/partnerzone/downloadarea/school-information/belgium/index.aspx">Belgium</a>
    </h2>            
    <p>Land of beer and food!</p>        
   </div>
  </div>
  <script src="./content/js/jquery-1.2.6.min.js" type="text/javascript"></script>
  <script src="./content/js/dupetargets.js" type="text/javascript"></script>
 </body>
</html>
borisCallens
Can you please make it more clear as I am new to jquery
MKS
gimme a sec, I'll write you something up
borisCallens
One thing I missed in above question that there are three div id "txtCss" as well as three div ID "txtLink" and if I am using your code then it is showing error on "$" saying Error: $ is not definedSource File: http://cmsstag/js/manoj.jsLine: 1
MKS
the $ is from jquery. Make sure you are importing the jquery library.There can never be multiple nodes with the same id. Either you have to give them unique id's or you have to remove the id and use classes.The script could be altered to accept the threeimages classed nodes and only work in that node.
borisCallens
how can I use this code for the "classes" please give my the link of importing jquery.
MKS
hi Boris can you please write sample code for me using classes not ID and also what i need to add to remove error "$ is not defined"
MKS
I am using this link to add the jquery http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js what you say?
MKS
Firstly: I updated the HTML and JS to use the classes. Now simply giving a node a class "threeimages" and giving it the same structure as you gave as an example will duplicate the link.Where you get your jquery plugin librar doesn't really matter, as long as the url you reference at the bottom of the HTML I showed you references the same path.
borisCallens
A: 

You can use:

var txtLink = document.getElementById('txtLink');
var a = txtLink.getElementsByTagName('a');
if (a != null && a.length > 0) {
    var setLink = txtLink.parentNode.getElementsByTagName('a');
    if (setLink != null && setLink.length > 0) {
        setLink[0].href = a[0].href;
    }
}

I think that this should work..

José Leal
Dear jose its giving error that "txtLink" is null any idea
MKS
+4  A: 

this is the shortest answer without using any library and works only thing what you want

var tc = document.getElementById("txtCss");
var ary = tc ? tc.getElementsByTagName("a") : [];
if(ary.length >= 2)
    ary[0].href = ary[1].href;
Tolgahan Albayrak
+1 for tc ? tc.getElementsByTagName() : []
Grant Wagner
A: 

This is a simple understandable n00b like code ...

 <html>
  <head>
   <script language="javascript">  
  function simpleChangeURL(){
        var anchors = document.getElementsByTagName('a');
        if(anchors != null & anchors.length > 0){
         anchors[0].href = anchors[1].href; 
        } 
    }

    function simpleChangeByAustraliaURL()
    {
        var anchors = document.getElementsByTagName('a');
        var images = document.getElementsByTagName('img');
        var imageNeeded;
        var anchorNeeded;
        if(images != null){
         for( var i = 0; i < images.length ; i++){
          if (images[i].alt == 'Australia') imageNeeded = images[i];
         }
        }
        if(anchors != null){
         for( var j = 0; j < anchors.length ; j++){
          if (anchors[i].firstChild.data == 'Australia') anchorNeeded = anchors[i];
         }
        }
        if(imageNeeded != null && anchorNeeded!= null){
 var imageAnchor = imageNeeded.parentNode;
 imageAnchor.href = anchorNeeded;

}

    }


    </script>
    </head>

    <body>
    <div class="threeimages" id="txtCss">  
    <a>   
           <img alt="Australia" src="/Images/Services%20button_tcm7-9688.gif"/>
    </a>        
        <div class="text" id="txtLink" >            
            <h2>
                    <a href="/partnerzone/downloadarea/school-information/australia/index.aspx">Australia</a>
            </h2>            
            <p>Land of the sunshine!</p>        
        </div>
    </div>
    <script> simpleChangeByAustraliaURL();</script>
    </body>
    </html>