0

I am creating a link gallery.

HTML:

<div class="link">
    <header>Test Header</header>
    <a href="http://www.google.com/">Click Here</a>
</div>

I want to go to the A element's URL when clicking anywhere in the div.

Javascript with JQUERY 3.2.1:

$(function() {
    $("div.link").click(function() {
        $url = $this.children("a").attr("href");
        alert($url);
    });
});

It doesn't alert the URL

I have also tried:

$(function() {
    $("div.link").click(function() {
        $url = $this.find("a:first-child").attr("href");
        alert($url);
    });
});

It returns a collection instead of a single element so I tried the following:

$function() {
    $("div.link").click(function() {
        $url = $this.find("a");
        alert($url[0].attr("href"));
    });
});

Neither approach is working. I have confirmed the click selector is functioning by testing it with a simple alert("hello");

EDIT: I was using $this instead of $(this).

3
  • 3
    Try using: $(this) Commented Dec 5, 2017 at 16:16
  • 1
    $this isn’t defined. Use the browser console (dev tools) (hit F12) and read any errors. Commented Dec 5, 2017 at 16:19
  • oh wow. I cannot believe I made such a stupid mistake. Of course it is $(this). I am an idiot. I have been developing in PHP so much lately it must have corrupted my mind. Commented Dec 5, 2017 at 16:20

3 Answers 3

1

I want to goto the A element's URL when clicking anywhere in the div.

Based on the code you have shared, $this seems to be undefined

If you just want to fetch the href property of the child anchor tag, then simply

$("div.link").click(function() {
    var href = $(this).find("a").attr( "href" ); //attr will fetch the attribute of first element in the result of find
});

If you want to click on the child anchor tag, then try using one of the following ways

$("div.link").click(function() {
    $(this).find("a").first().click();
});

or

$("div.link").click(function() {
    $(this).find("a").click();
});

or

$("div.link").click(function() {
    $(this).find("a").trigger( "click" );
});

or if you just want to go the URL

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

2 Comments

This is correct. I don't know what I was thinking with $this. I have no idea. I am selecting this as the answer in 7 minutes when I can. Dumb question.
@Velocibadgery Try using developer-tools of a browser more often (like console, element-inspector) - this kind of issues will become easier to spot.
0

If you want to get the href attribute when clicking on the div element, you can consider using the selector $(".nameOfDivClass"). On the element selected you will bind a function that will get your href.

$(".link").on("click", function(){
 console.log($(this).children("a").attr("href"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="link">
    <header>Test Header</header>
    <a href="http://www.google.com/">Click Here</a>
</div>

Comments

0

If you check your browser's console, you'll have an error saying $this is not defined. It should be $(this), in order to turn this (the raw HTML element clicked on) into a jQuery object on which you can the run jQuery methods such as .find() and .attr().

Additionally, this version will actually cause the navigation action rather than just alerting the URL:

$("div.link").click(function() {
    $(this).find("a").click();
});

Comments

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.