Search code examples
javascriptjqueryhtmlhreftarget

Jquery find link in Div - open as defined in html


I need some div's to work as a clickable link. I already found the solution for jquery. Unfortunately this solution is always window.location or window.open. This is not suitable for us, since we have a lot of div's and the urls are already defined with _blank or same window.

I have to admit, that I am not sure, how I can look for the function that I need, since I have almost no knowledge of Javascript and the functions of it.

This is the code for the script, that I found:

$(document).ready( function () {
$(".textlink").click(function () {
        window.location = $(this).find("a:first").attr("href");
        return false;
    });  
});

and this is one box with target=_blank

<div class="textlink texticon texticon-top"><a href="http://term1caq/WebCAQ.Net/App.QBD/DocumentView.aspx?id=429" target="_blank">Text in DIV</a></div>

The expected result would be, that the whole div is clickable but the target will be taken from the href of the div and not predefined in the script.


Solution

  • You just need to trigger click on found anchor tag instead of setting its href to window.location

    $(document).ready( function () {
        $(".textlink").click(function () {
            $(this).find("a:first").click();
        });  
    });