SMacFadyen
SMacFadyen

Reputation: 3165

Get a href attr and set it to another

<script type="text/javascript">
$(function() {

    $('.thelink a').select(function() {

        var a_href = $(this).attr("href");

    });
    $('.calltoActionMoreInformation a').attr('href', a_href);
});
</script>

  <div class="thelink" style="height: 250px; position: relative;">
  <a title="xxx" href="xxx">
    <img alt="tree" src="x" />

  </a>
  </div>

Trying to put the href from a inside into:

  <span class="calltoActionMoreInformation" style="position: absolute; bottom: 0px;">
   <a title="" href="--Link here--"></a>
  </span>

If I set var a_href = 'http://www.google.co.uk'; it sets it correctly, so the problem lies within getting the href of the only a link within .thelink div..

How do I assigned the href in .thelink a to .calltoActionMoreInformation a ?

Upvotes: 0

Views: 183

Answers (3)

The System Restart
The System Restart

Reputation: 2881

$('.thelink a').click(function() {
   var a_href = $(this).attr("href");
   $('.calltoActionMoreInformation a').attr('href', a_href);
   return;
});

Upvotes: 0

Mouloud
Mouloud

Reputation: 3795

1 - the variable "a href" is not accessible from the global context, but only the select function.

2 - why use "select" function "click" is more appropriate in this case

correction :

$(function(){

    $('.thelink a').click(function(e) {
        $('.calltoActionMoreInformation > a').attr('href', $(this).attr('href'));
            e.preventDefault(); // or
        return false; // this is important is you dont want the browserfollow the link
    });

});

is it right ?

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

$('.thelink a').click(function(e) {
   e.preventDefault();
   var a_href = $(this).attr("href"); // or this.href
   $('.calltoActionMoreInformation a').attr('href', a_href);
});

DEMO

Upvotes: 7

Related Questions