0

I have a href url when I clicked that function I need to get some information but I oncliking the href Jquery function is not being called

Jquery code

$("#displayfood").click(function(){
    console.log("Function called")
});

Html code

<div class="panel-heading">
    <h4 class="panel-title">
        <a href="#collapse1" id="displayfood" data-parent="#accordion2" 
           data-toggle="collapse" class="btn-block collapsed"> 
            <img class="img-rounded" src="images/db-icons/default-food.png"> Pizza
        </a>
    </h4>
</div>
4
  • did you include the jQuery library? Commented Oct 27, 2014 at 7:37
  • wrap it in dom ready Commented Oct 27, 2014 at 7:37
  • Are you using any other tab function for tab view. ? Commented Oct 27, 2014 at 7:40
  • plnkr.co/edit/NEAo6I38llqaz1i1YqfH?p=preview Commented Oct 27, 2014 at 7:41

7 Answers 7

1

In the function you should use: "return" false or "event.preventDefault()" to stop the default event of click.

like this :

$("#displayfood").click(function(){
       console.log("Function called")
       return false
 });

or:

$("#displayfood").click(function(e){
       console.log("Function called")
       e.preventDefault()
 });
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

$(document).ready(function(){
   $("#displayfood").click(function(e){
      e.preventDefault();
      console.log("Function called");
    });
});

Comments

0

Where did you put the jquery code? It needs to be after where you have defined the displayfood, otherwise that element will not be found when code executes. Alternatively, use the ready function.

Comments

0

You need to bind the event listener on document.ready.

$(document).ready(function() {
    $("#displayfood").click(function(){
       console.log("Function called")
    });    
});

    $(document).ready(function() {
        $("#displayfood").click(function(){
           console.log("Function called")
        });    
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel-heading">
     <h4 class="panel-title">
                                            <a href="#collapse1" id="displayfood" data-parent="#accordion2" data-toggle="collapse" class="btn-block collapsed"> <img class="img-rounded" src="images/db-icons/default-food.png"> Pizza
                                            </a>
                                        </h4>

</div>

Comments

0

Your code works fine, just ensure following things.

  1. Jquery is loaded
  2. Close <img> tag with </img>
  3. Wrap js code inside document.ready

Reference: http://jsfiddle.net/fqLk9n02/

Comments

0

If you use bootstrap because I saw the same structure but used wrong, check my example and on click works just fine ;)

<div class="panel panel-info">
  <div class="panel-heading">
     <a href="#collapse1" id="displayfood" data-parent="#accordion2" 
       data-toggle="collapse" class="btn-block collapsed"> 
           <img class="img-rounded" src="images/db-icons/default-food.png"/> Pizza
    </a>
  </div>
  <div class="panel-body">
    Panel content
  </div>
</div>

$("#displayfood").click(function(){
    alert("Function called")
});

FIDDLE:

Comments

0

jquery .click is shorthand for .on("click") so use the .on("click");

$("#displayfood").on("click",function(){
     console.log("Function called")
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.