0

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

4
  • Do you have any further info on this problem? Using your examples, it works perfectly for me - both the cursor and link in both IE and Firefox. Is your jQuery library correctly referenced on the page? Commented Aug 3, 2010 at 7:52
  • that should work, demo Commented Aug 3, 2010 at 7:53
  • I'm on chrome too... latest version and that works... was my demo not working in your browser? Commented Aug 3, 2010 at 7:59
  • see my example, it should work Commented Aug 3, 2010 at 8:04

6 Answers 6

1

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

$(function(){
  $('#logo').bind('click',function() {
    window.location = "index.php"
  });
});
Sign up to request clarification or add additional context in comments.

4 Comments

@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: api.jquery.com/ready
@Jean: You have not specified the ready handler, note my first line please $(function(){
@sAc, I dont think this is needed in this case, because see my example it is working without the wrapping
@Starx: jsfiddle puts your code in ready handler implicitly when using jquery. Note from the left selection bar you have chosen load event.
1

add display:block; to your css

see an working example here

http://jsfiddle.net/4ceK4/

Comments

1

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.

Comments

0

Use,

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

instead of

 window.location = "index.php"

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

Comments

0

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 */
}

Comments

0

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

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.