Are you first timer at lazacode.org? Why not join us and start exchange your knowledge with everyone. Register today!

jQuery .click() method

0 votes
6 views
added Feb 21 in jQuery by birdygeek Sergeant (2,020 points)
edited Apr 2 by LC Marshal

To check which id has been clicked  

$('#btn1, #btn2').click(function () {
   if (this.id == 'btn1') {
      alert('btn1 clicked');
   }
   else if (this.id == 'btn2') {
      alert('btn2 clicked');
   }
});

5 examples

0 votes
responded Apr 2 by LC Marshal Second Warrant Officer (6,010 points)
edited Apr 3 by LC Marshal

Alert when click #target

$( "#target" ).click(function() {
alert( "Handler for .click() called." );
});
0 votes
responded Apr 2 by LC Marshal Second Warrant Officer (6,010 points)

Click on #WhenClickThis will hide the #HideThis and show the #ShowThis

$("#WhenClickThis").click(function () {
    $("#HideThis").hide();
    $("#ShowThis").fadeIn('500');
});
0 votes
responded Apr 2 by LC Marshal Second Warrant Officer (6,010 points)
edited Apr 3 by LC Marshal

Click .button will have smooth scrolling to .scrolltothis element

$(".button").click(function() {
    $('html,body').animate({
        scrollTop: $(".scrolltothis").offset().top},'slow');
});
0 votes
responded Apr 3 by LC Marshal Second Warrant Officer (6,010 points)

Show and hide upon click with css properties

$("#NextButton").click(function() {
    $("#Page1").css("display", "block");
    $("#Page0").css("display", "none");
});
0 votes
responded Apr 5 by LC Marshal Second Warrant Officer (6,010 points)

Use the javascript variables as a parameter in a jquery selector

$("input").click(function(){
    var name = $(this).attr("name");
    $('input[name="' + name + '"]').hide();    
}); 
lazacode.org - Malaysia based Knowledge Exchange Platform, where you can share your finding or solution with other members of community.
...