Click here to Skip to main content
15,920,005 members
Home / Discussions / JavaScript
   

JavaScript

 
AnswerRe: Making two cards match and then disappear with JQuerry Pin
Christian75739-Apr-18 15:47
Christian75739-Apr-18 15:47 
I'm not sure what you're doing with the images, but I would do this:
Set an attribute on each of your .card elements perhaps called matchNum. (If you have to set this via jQuery you can use the .attr() function)

Then I would use something like this:
JavaScript
var otherSelectedCard = null; //Place to store a clicked-on card
$(".deck").on("click", ".card", function() {
  
  //If the user clicked the same card twice, "unselect" the card
  if (otherSelectedCard === this) {
    $(this).find("img").hide();
    otherSelectedCard = null;
    return;
  }

  //Check to see if a card is already selected
  if (otherSelectedCard !== null) {
    //One is, check if their a match
    if ($(otherSelectedCard).attr("matchNum").toString() === $(this).attr("matchNum").toString()) {
      //They are, make both cards disapear
      $(this).hide();
      $(otherSelectedCard).hide();
      otherSelectedCard = null;
    }
    else { //They're not, show the image, then hide both images
      $(this).find("img").show();
      var thisVar = this, otherSelectedCard2 = otherSelectedCard;
      setTimeout(function() {
        $(thisVar).find("img").hide();
        $(otherSelectedCard2).find("img").hide();
      }, 500);
      otherSelectedCard = null;
    }
  }
  else { //No card is selected yet, select the current one
    $(this).find("img").show();
    otherSelectedCard = this;
  }

});


This is my first time writing jQuery in a while so I probably goofed up somewhere in there. Hope this gives you a general idea of a solution here though Smile | :)
QuestionNewbie: how do I analyse DOM Objects? Pin
enginestar5-Apr-18 3:05
enginestar5-Apr-18 3:05 
AnswerRe: Newbie: how do I analyse DOM Objects? Pin
Richard MacCutchan5-Apr-18 4:34
mveRichard MacCutchan5-Apr-18 4:34 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
enginestar5-Apr-18 4:50
enginestar5-Apr-18 4:50 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
Richard MacCutchan5-Apr-18 6:10
mveRichard MacCutchan5-Apr-18 6:10 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
enginestar5-Apr-18 6:57
enginestar5-Apr-18 6:57 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
Richard MacCutchan5-Apr-18 7:00
mveRichard MacCutchan5-Apr-18 7:00 
GeneralRe: Newbie: how do I analyse DOM Objects? Pin
Christian75739-Apr-18 15:51
Christian75739-Apr-18 15:51 
QuestionisAuthenticated() always returns false irrespective whether user is logged in or not Pin
Member 137573661-Apr-18 22:55
Member 137573661-Apr-18 22:55 
QuestionHow do i create an apps using android studio? Pin
Siti nasuha28-Mar-18 16:12
Siti nasuha28-Mar-18 16:12 
QuestionSetting a variable to a string..Do.. While loop Pin
esljustin26-Mar-18 14:15
esljustin26-Mar-18 14:15 
AnswerRe: Setting a variable to a string..Do.. While loop Pin
Richard MacCutchan26-Mar-18 22:15
mveRichard MacCutchan26-Mar-18 22:15 
AnswerRe: Setting a variable to a string..Do.. While loop Pin
Weavr8-Apr-18 0:39
Weavr8-Apr-18 0:39 
Questionany shortcut to toggle in and out the jquery code Pin
indian14323-Mar-18 8:43
indian14323-Mar-18 8:43 
QuestionText to Image Font Size Pin
Member 1374210822-Mar-18 17:51
Member 1374210822-Mar-18 17:51 
AnswerRe: Text to Image Font Size Pin
Richard Deeming23-Mar-18 2:46
mveRichard Deeming23-Mar-18 2:46 
QuestionHow to fetch unchecked checkbox value Pin
Member 1372997516-Mar-18 1:25
Member 1372997516-Mar-18 1:25 
AnswerRe: How to fetch unchecked checkbox value Pin
Richard Deeming16-Mar-18 2:50
mveRichard Deeming16-Mar-18 2:50 
AnswerRe: How to fetch unchecked checkbox value Pin
F-ES Sitecore22-Mar-18 6:58
professionalF-ES Sitecore22-Mar-18 6:58 
PraiseRe: How to fetch unchecked checkbox value Pin
Richard Deeming22-Mar-18 9:14
mveRichard Deeming22-Mar-18 9:14 
GeneralRe: How to fetch unchecked checkbox value Pin
F-ES Sitecore22-Mar-18 23:15
professionalF-ES Sitecore22-Mar-18 23:15 
QuestiontoggleClass and hasClass Pin
VK1915-Mar-18 4:31
VK1915-Mar-18 4:31 
AnswerRe: toggleClass and hasClass Pin
Richard Deeming15-Mar-18 6:35
mveRichard Deeming15-Mar-18 6:35 
GeneralRe: toggleClass and hasClass Pin
VK1915-Mar-18 7:56
VK1915-Mar-18 7:56 
GeneralRe: toggleClass and hasClass Pin
Richard Deeming15-Mar-18 8:03
mveRichard Deeming15-Mar-18 8:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.