tags:

views:

38

answers:

6

Hello,

I have put a logo as the background image in a div

<div id="logo" style="background-image:url(images/logo.jpg); position:absolute; top:20px; left:18%; width:275px; height:100px; cursor:pointer;"></div>

I want to use this as div as a link using jquery

//jQuery code

$('#logo').bind('click',function() {
window.location = "index.php"
});

Neither does the cursor:pointer show up nor does the link work, any ideas.

Thanks Jean

A: 

Use,

 window.location.replace("index.php")

instead of

 window.location = "index.php"

and instead of .bind, just use .click event.

Manie
A: 

You don’t need jQuery/JavaScript for that, just use HTML and CSS.

<a href="index.php" id="logo">Blabla</a>

#logo {
 display: block;
 background-image: url(img/foo.png);
 text-indent: -9999em;
 overflow: hidden;
 width: 300px; /* width of image goes here */
 height: 300px; /* height of image goes here */
}
Mathias Bynens
A: 

looks like you are missing a semicolon after "index.php"

antonlavey
semicolon is not the problem right now.. ;)
Reigel
+1  A: 

Register your click event handler in the ready event handler as follows, and it will work:

$(document).ready(function() {
    $("#logo").click(function() {
        window.location = "index.php";
    });
});

Regarding your CSS problem, I've tested it using Google Chrome (5.0.375.125), Opera (10.60), and Internet Explorer (8.0), and the cursor is displayed correctly.

Giu
+1  A: 

Make sure that you wrap your code in ready handler like this:

$(function(){
  $('#logo').bind('click',function() {
    window.location = "index.php"
  });
});
Sarfraz
any particular reason, normally thats the code I write
Jean
@Jean: You need to specify wrap your code in ready handler so that when DOM becomes ready, your code should become runable when for example the logo gets clicked. More Info: http://api.jquery.com/ready/
Sarfraz
I dont understand, how different is your code from mine
Jean
@Jean: You have not specified the ready handler, note my first line please `$(function(){`
Sarfraz
@sAc, I dont think this is needed in this case, because see my example it is working without the wrapping
Starx
@Starx: jsfiddle puts your code in ready handler implicitly when using jquery. Note from the left selection bar you have chosen load event.
Sarfraz
+1  A: 

add display:block; to your css

see an working example here

http://jsfiddle.net/4ceK4/

Starx
div is already a blocked element by default ;)
Reigel
I know but it worked when I placed it, weird
Starx
yeah! and still working when I removed it.
Reigel
This is just WEIRD
Starx