jQuery .click() method

0 votes
2,957 views
added Feb 21, 2018 in jQuery by anonymous
edited Jul 12, 2018 by LC Marshal
  1. //To check which id has been clicked
  2. $('#btn1, #btn2').click(function () {
  3. if (this.id == 'btn1') {
  4. alert('btn1 clicked');
  5. }
  6. else if (this.id == 'btn2') {
  7. alert('btn2 clicked');
  8. }
  9. });

18 Responses

0 votes
responded Apr 2, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. // Alert when click #target
  2. $( "#target" ).click(function() {
  3. alert( "Handler for .click() called." );
  4. });
0 votes
responded Apr 2, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. // Click on #WhenClickThis will hide the #HideThis and show the #ShowThis
  2. $("#WhenClickThis").click(function () {
  3. $("#HideThis").hide();
  4. $("#ShowThis").fadeIn('500');
  5. });
0 votes
responded Apr 2, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. //Click .button will have smooth scrolling to .scrolltothis element
  2. $(".button").click(function() {
  3. $('html,body').animate({
  4. scrollTop: $(".scrolltothis").offset().top},'slow');
  5. });
0 votes
responded Apr 3, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. //Show and hide upon click with css properties
  2. $("#NextButton").click(function() {
  3. $("#Page1").css("display", "block");
  4. $("#Page0").css("display", "none");
  5. });
  6.  
  7. //Example #2
  8. $('.show-header-search').click(function() {
  9. $('.header-search').fadeIn(200);
  10. });
0 votes
responded Apr 5, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. //Use the javascript variables as a parameter in a jquery selector
  2. $("input").click(function(){
  3. var name = $(this).attr("name");
  4. $('input[name="' + name + '"]').hide();
  5. });
0 votes
responded May 3, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. //Click close button and slideUp() the parent element
  2. $('.forum-close-btn').click(function(){
  3. $('#forum-how-it-works').slideUp(500);
  4. });
0 votes
responded May 4, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. //click element bring to specific link
  2. $("button.news").click(function(){
  3. window.location ="news?combine=&field_category_value=news#main-content";
  4. return false;
  5. });
  6. //Example #2
  7. $(".header-search .btn-header-search").click(function() {
  8. var searchterm = $(".header-search input").val();
  9. if (searchterm !== "") {
  10. window.location = "/projects?keyword=" + searchterm;
  11. }
  12. });

 

0 votes
responded May 4, 2018 by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal
  1. //Change Text with Click Events
  2. $(document).ready(function() {
  3. $("#getMessage").on("click", function(){
  4. $(".message").html("Here is the message");
  5. });
  6. });

 

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

 

0 votes
responded Oct 31, 2018 by LC Marshal Captain (25,790 points)
reshown Aug 9, 2019 by LC Marshal
  1. $("#cb_tab_Flight").on("click", function() {
  2. window.gtmDataLayer.push( {
  3. event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "flight", "eventDetails.nonInteraction": !0
  4. }
  5. )
  6. }
  7.  
  8. );
  9. $("#cb_tab_Hotel").on("click", function() {
  10. window.gtmDataLayer.push( {
  11. event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "hotel", "eventDetails.nonInteraction": !0
  12. }
  13. )
  14. }
  15.  
  16. );
  17. $("#cb_tab_Package").on("click", function() {
  18. window.gtmDataLayer.push( {
  19. event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "flight+hotel", "eventDetails.nonInteraction": !0
  20. }
  21. )
  22. }
  23.  
  24. );
  25. $("#cb_tab_Car").on("click", function() {
  26. window.gtmDataLayer.push( {
  27. event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "car", "eventDetails.nonInteraction": !0
  28. }
  29. )
  30. }
  31.  
  32. );
  33. $("#cb_tab_Activities").on("click", function() {
  34. window.gtmDataLayer.push( {
  35. event: "trackEventNoEcommerce", "eventDetails.category": "hpsearch widget", "eventDetails.action": "click", "eventDetails.label": "activities", "eventDetails.nonInteraction": !0
  36. }
  37. )
  38. }
  39.  
  40. );

 

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

 

0 votes
responded Jul 9, 2019 by LC Marshal Captain (25,790 points)
  1. function showBtnDefault () {
  2. $(shareBtn).removeClass('on-click');
  3. $(btnIco).attr('src', defIco);
  4. $(shareBox).removeClass('in');
  5. }
  6.  
  7. var open = false;
  8. $(shareBtn).click(function() {
  9. open = !open;
  10.  
  11. if(open) {
  12. // console.log('11');
  13. } else {
  14. showBtnDefault();
  15. }
  16. });

 

0 votes
responded Aug 7, 2019 by lcjr Lieutenant (12,460 points)
  1. $("button.news").click(function(){
  2. window.location ="news?combine=&field_category_value=news#main-content";
  3. return false;
  4. });

 

0 votes
responded Aug 8, 2019 by lcjr Lieutenant (12,460 points)
  1. var clear = $('.tm-actions .clear');
  2. clear.on('click',function(){
  3. $('.tm-lines input:checkbox').prop('checked', false);
  4. button.removeClass('active');
  5. $('span.selected').text('0');
  6. });

 

0 votes
responded Aug 8, 2019 by lcjr Lieutenant (12,460 points)
  1. $('#search_listing').on('click', function(){
  2. var listing_type = $('#listing_type li.active').attr('data'),
  3. radius = $('#radius li.active').attr('data'),
  4. property_type= $('#property_type li.active').attr('data');
  5. if($(this).hasClass('active')){
  6. var lat_val = [],
  7. lon_val = [],
  8. station = [];
  9. $('.tm-line-content').find('.tm-station input[type=checkbox]:checked').each(function(i){
  10. lat_val[i] = $(this).data('lat');
  11. lon_val[i] = $(this).data('lon');
  12. station[i] = $(this).data('station');
  13. });
  14. 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;
  15. // console.log(link);
  16. window.open(link,'_blank');
  17. }
  18. });

 

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

 

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