0

I have this HTML:

<div class="divHolder">
   <a href="./"><img src="./images/image6.jpg"/></a>
</div>
<div class="divHolder">
   <a href="./IT.aspx"><img src="./images/image7.jpg"/></a>
</div>

In my JavaScript I have:

$('.divHolder').click(function () {

    var link = $(this).find("a").attr("href");

    if (link != null) {
         location.href = link;
    }
});

However, every time I click on a div, the href of the div is always the last one: for example in this case is: IT.aspx.

What did I do wrong?

2
  • Its working fine for me... Check jsfiddle.net/BSZF7 Commented Jun 2, 2011 at 17:51
  • Hmm. Works for me, apart from that link is of course never null. Have you tried alert ing link rather than immediately forwarding to it? I'm just wondering whether it's just that the default page, if that's the correct term, is IT.aspx and essentially in the first instance all you're doing is stripping the url down to the last directory which will then load the default page in that directory. Commented Jun 2, 2011 at 17:52

1 Answer 1

1

A better way to do this would be the following

$('.divHolder:has(a)').click(function () {
  location.href = $(this).find("a").attr("href");
});
Sign up to request clarification or add additional context in comments.

1 Comment

I find it strange, but I fixed this problem in my javascript. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.