0

I have a div declared as follow, but the click event doesn't work. I also need to use the :hover event in the css, but it doesn't work as well. What's the problem ?

<div id="info-button" class="info-button"></div>

the css

.info-button {
    position:absolute;
    top:-200px;
    left:50px;
    background-image: url('../img/info-icon.png');
    background-size: 100%;
    background-position: 0px 0px;
    width: 89px !important;
    display: inline-block;
    height: 89px;
    background-repeat: no-repeat;
}

JQuery

$("#info-button").click(function() {
    alert("click");  
});

I also tried

$("#info-button").on("click", function () {
    alert("click");
});
6
  • Any errors in the console? Commented Aug 19, 2015 at 9:07
  • Are you registering click event in (document).ready or not ? Commented Aug 19, 2015 at 9:09
  • have you import jQuery Library ? Commented Aug 19, 2015 at 9:10
  • $(function(){ $("#info-button").click(function() { alert("click"); }); }); Commented Aug 19, 2015 at 9:12
  • Your code works. Error is elsewhere. CSS Hover needs : '.info-button:hover { background-color:blue;} Commented Aug 19, 2015 at 9:18

3 Answers 3

1

Be sure to put your code inside document.ready

$( document ).ready(function() {
   $("#info-button").click(function() {
    alert("click");  
    });
});

And include properly your script in your HTML document after Jquery, example:

<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/your_script.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Based on placement of code, document.ready is not requirement, unless code is defined before the BODY itself
The problem was the document.ready ! Incredible. But the mouse hover event in the css doesn't work anyway. Another kind of problem for sure but I haven't solved it yet.
1

Works perfectly fine. See the JSBin.

http://jsbin.com/lusiyalusu/edit?html,js,output

EDIT: Just saw you CSS, I think your DIV might be drawing outside the body, if its parent DIV. You've top set to negative value in absolute postioning.

.info-button {
    position:absolute;
    top:-200px;
    left:50px;
    background-image: url('../img/info-icon.png');
    background-size: 100%;
    background-position: 0px 0px;
    width: 89px !important;
    display: inline-block;
    height: 89px;
    background-repeat: no-repeat;
}

You can check if this is the case

1 Comment

then you must not include JavaScript before body, it's blocking and essentially bad practise to do
0

Working Example

$("#info-button").click(function() {
    alert("click");  
});
$("#info-hover").hover(function() {
    alert("hover");  
});
.info-button {
    background-color: #f0f0f0;
    background-image: url("../img/info-icon.png");
    background-position: 0 0;
    background-repeat: no-repeat;
    background-size: 100% auto;
    display: inline-block;
    height: 89px;
    
    
    width: 89px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="info-button" class="info-button"></div>

<div id="info-hover" class="info-button"></div>

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.