0

I'm trying to give some functionality to my button using jquery click function. But when i run the code the function wouldn't get invoke at all.

My code is:

 <script type="text/javascript">
 $(function () {
     $("#min").click(function(e){
         $("#listFriends").css('display','none');
         $("#friendsO").show();
     });
 </script>

And

<button id="min">[-]</button>

Please anyone tell me what mistake I made in this code.....Thanks..........

5
  • Does the button exists when you are binding the event? Commented Feb 23, 2013 at 21:15
  • @Jean-Philippe Leclerc yes otherwise how I click on the button Commented Feb 23, 2013 at 21:19
  • You could load the button in ajax or added to the dom AFTER the event is bound to the element. Commented Feb 23, 2013 at 21:19
  • Would you paste your whole page? Commented Feb 23, 2013 at 21:23
  • @Jean-Philippe Leclercr below answer was solved my problem Thanks........ Commented Feb 23, 2013 at 21:31

2 Answers 2

3

You are missing }); for closing document ready handler.

$(function() {
   $("#min").click(function(e){
      console.log('test');
      $("#listFriends").hide();
      $("#friendsO").show();
   });
}); // <----

If you generate the element dynamically you should delegate the event, from one of static parents of the element or document object:

$(document).on('click', "#min", function(e){
      console.log('test');
      $("#listFriends").hide();
      $("#friendsO").show();
});
Sign up to request clarification or add additional context in comments.

3 Comments

@james Do you see any error on the console? Have you included jQuery library?
No errors nothing... I'm adding this button with jQuery.. Is that cause anything for this problem...
@james You are welcome. Because when you try to select the element that element doesn't exist on the page.
0

Install firebug on firefox and enable debug, see here.

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.