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
27 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');
   }
});

9 examples

0 votes
responded Apr 2 by LC Marshal First Warrant Officer (8,170 points)
edited May 4 by LC Marshal
// Alert when click #target
$( "#target" ).click(function() {
alert( "Handler for .click() called." );
});
0 votes
responded Apr 2 by LC Marshal First Warrant Officer (8,170 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 First Warrant Officer (8,170 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 First Warrant Officer (8,170 points)
edited May 4 by LC Marshal

Show and hide upon click with css properties

$("#NextButton").click(function() {
    $("#Page1").css("display", "block");
    $("#Page0").css("display", "none");
});
Example #2
$('.show-header-search').click(function() {
  $('.header-search').fadeIn(200);
});
0 votes
responded Apr 5 by LC Marshal First Warrant Officer (8,170 points)

Use the javascript variables as a parameter in a jquery selector

$("input").click(function(){
    var name = $(this).attr("name");
    $('input[name="' + name + '"]').hide();    
}); 
0 votes
responded May 3 by LC Marshal First Warrant Officer (8,170 points)

Click close button and slideUp() the parent element

$('.forum-close-btn').click(function(){
  $('#forum-how-it-works').slideUp(500);
}); 
0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)

click element bring to specific link

$("button.news").click(function(){
    window.location ="news?combine=&field_category_value=news#main-content";
    return false;
});
 
Example #2
$(".header-search .btn-header-search").click(function() {
  var searchterm = $(".header-search input").val();
  if (searchterm != "") {
    window.location = "/projects?keyword=" + searchterm;
  }
});
0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)
//Change Text with Click Events
$(document).ready(function() {
  $("#getMessage").on("click", function(){
  $(".message").html("Here is the message");
  });
});

 

0 votes
responded May 4 by LC Marshal First Warrant Officer (8,170 points)
//Add/remove class with click and mouse event
$(".project-details-nav li").click(function() {
  $("#facilities-tab").addClass("p-t-90");
});
lazacode.org - Malaysia based Knowledge Exchange Platform, where you can share your finding or solution with other members of community.
...