jQuery .click() method

0 votes
2,074 views
added Feb 21, 2018 in jQuery by anonymous
edited Jul 12, 2018 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');
   }
});

18 Responses

0 votes
responded Apr 2, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
// Alert when click #target
$( "#target" ).click(function() {
alert( "Handler for .click() called." );
});
0 votes
responded Apr 2, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
// Click on #WhenClickThis will hide the #HideThis and show the #ShowThis
$("#WhenClickThis").click(function () {
    $("#HideThis").hide();
    $("#ShowThis").fadeIn('500');
});
0 votes
responded Apr 2, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 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, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 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, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//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, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Click close button and slideUp() the parent element
$('.forum-close-btn').click(function(){
  $('#forum-how-it-works').slideUp(500);
}); 
0 votes
responded May 4, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//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, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Change Text with Click Events
$(document).ready(function() {
  $("#getMessage").on("click", function(){
  $(".message").html("Here is the message");
  });
});

 

0 votes
responded May 4, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//Add/remove class with click and mouse event
$(".project-details-nav li").click(function() {
  $("#facilities-tab").addClass("p-t-90");
});
0 votes
responded Jun 29, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
//click and slideup animation
$( "p" ).click(function() {
  $( this ).slideUp();
});
0 votes
responded Aug 21, 2018 by LC Marshal Captain (25,790 points)
$('#submit-info').click(function () {
  $('#personal-information').hide();
  $('#verify-identity').show();
});

 

0 votes
responded Oct 31, 2018 by LC Marshal Captain (25,790 points)
reshown Aug 9, 2019 by LC Marshal
$("#cb_tab_Flight").on("click", function() {
    window.gtmDataLayer.push( {
        event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "flight", "eventDetails.nonInteraction": !0
    }
    )
}

);
$("#cb_tab_Hotel").on("click", function() {
    window.gtmDataLayer.push( {
        event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "hotel", "eventDetails.nonInteraction": !0
    }
    )
}

);
$("#cb_tab_Package").on("click", function() {
    window.gtmDataLayer.push( {
        event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "flight+hotel", "eventDetails.nonInteraction": !0
    }
    )
}

);
$("#cb_tab_Car").on("click", function() {
    window.gtmDataLayer.push( {
        event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "car", "eventDetails.nonInteraction": !0
    }
    )
}

);
$("#cb_tab_Activities").on("click", function() {
    window.gtmDataLayer.push( {
        event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "activities", "eventDetails.nonInteraction": !0
    }
    )
}

);

 

0 votes
responded Apr 16, 2019 by LC Marshal Captain (25,790 points)
// click event for more than 1 elements
$('.el1, .el2').on('click', some_function);

 

0 votes
responded Jul 9, 2019 by LC Marshal Captain (25,790 points)
function showBtnDefault () {
  $(shareBtn).removeClass('on-click');
  $(btnIco).attr('src', defIco);
  $(shareBox).removeClass('in');
}

var open = false;
$(shareBtn).click(function() {
  open = !open;

  if(open) {
    // console.log('11');
  } else {
    showBtnDefault();
  }
});

 

0 votes
responded Aug 7, 2019 by lcjr First Warrant Officer (11,530 points)
$("button.news").click(function(){
    window.location ="news?combine=&field_category_value=news#main-content";
    return false;
});

 

0 votes
responded Aug 8, 2019 by lcjr First Warrant Officer (11,530 points)
var clear = $('.tm-actions .clear');
clear.on('click',function(){
  $('.tm-lines input:checkbox').prop('checked', false);
  button.removeClass('active');
  $('span.selected').text('0');
});

 

0 votes
responded Aug 8, 2019 by lcjr First Warrant Officer (11,530 points)
$('#search_listing').on('click', function(){
  var listing_type = $('#listing_type li.active').attr('data'),
      radius = $('#radius li.active').attr('data'),
      property_type= $('#property_type li.active').attr('data');
  if($(this).hasClass('active')){
    var lat_val = [],
        lon_val = [],
        station = [];
    $('.tm-line-content').find('.tm-station input[type=checkbox]:checked').each(function(i){
      lat_val[i] = $(this).data('lat');
      lon_val[i] = $(this).data('lon');
      station[i] = $(this).data('station');
    });
    var link = "/buy?state=Kuala Lumpur&listing_type=" + listing_type + "&property_type=" +property_type +"&search_by=mrt&search_by_distance="+ radius +"&lat=" + lat_val + "&lng=" +lon_val + "&coord_name="+ station;
    // console.log(link);
    window.open(link,'_blank');
  }        
});

 

0 votes
responded Aug 13, 2019 by LC Marshal Captain (25,790 points)
// click and go to url
$('#reg-next').on('click', function(e) {
  var page = $(this).data('page');
  window.location.href = page + '.html';
});

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...