How to link when clicking a div

Asked

Viewed 740 times

4

I forgot how to make links with jQuery that run when clicking on a div. The link I needed was with target Blank.

I found it from here but I didn’t understand the code and I couldn’t use it.

jquery('a[href="http://site.com"]').attr('target', '_blank'); 
  • 1

    This code picks up all links (tag <a href...>) and puts target Blank. It doesn’t look like what you want. What do you want to happen when you click the div?

  • 1

    When I click on the car div I want you to run a certain link on target Blank

1 answer

4


To open in a new window use window.open (as bfavaretto warned):

$('#minhadiv').click(function(){
    window.open('http://www.google.com');
});

To open in the same window use window.location:

$('#minhadiv').click(function(){
    window.location='www.google.com';
});

Example: http://jsfiddle.net/4Rq37/1/

  • 2

    To open in another window/tab: window.open('http://www.google.com');

  • 2

    I had forgotten the window.Location, thank you!

  • 2

    Don’t forget the CSS: #minhadiv { cursor: pointer; }. For reasons of usability!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.