user695868
user695868

Reputation:

Redirect based on div a href link

I'm looking for a way to redirect a page based on a div's a href link. If there is only one div on the page, then redirect to the a href link for that div.

JS

$(function () { 
    if ($('.div').length = 1) { alert('Just one Div'); } 
});

HTML

<div class="div"><div class="title"><a href="../posters/bicycles/default.html">Bicycle</a></div>

Upvotes: 1

Views: 552

Answers (2)

Niels
Niels

Reputation: 49949

you can do this, returns the number of DIV elements:

if( $("div").length == 1 )
{
    document.location = $("div a").attr("href");
}

Upvotes: 1

palaѕн
palaѕн

Reputation: 73966

Try this:

$('.div a').click(function (e) {
    e.preventDefault();
    window.location.href = $(this).attr('href');
});

Upvotes: 1

Related Questions