0

I'm making a cards game, so i need to check if 2 elements (array in table) are the same, i noticed that the only way to do that is with JQuery, but i'm new with these things, i barely control Javascript, jQuery is a hell for me.

My code looks like this

var twoace = "<img src='twoace.png' class='two'>";
var twogold = "<img src='twogold.png' class='two'>";
var onegold = "<img src='onegold.png' class='one'>";
var oneace = "<img src='oneace.png' class='one'>"

// i have a lot more code (sorry for my english) but this is the essential

//initial array
tablecards = [deckcard[0], deckcard[1], deckcard[2], deckcard[3]];
document.getElementById("table1").innerHTML = tablecards[0];
document.getElementById("table2").innerHTML = tablecards[1];
document.getElementById("table3").innerHTML = tablecards[2];
document.getElementById("table4").innerHTML = tablecards[3];

// here i add a card to the table
tablecards.push(handcard[0]);

//now i have this 
tablecards = [deckcard[0], deckcard[1], deckcard[2], deckcard[3], handcard[4]];
document.getElementById("table5").innerHTML = tablecards[4];

How do i check if "tablecards[4]" and "tablecards[1]" (or any other) has same class?

6
  • tablecards[4].className == tablecards[1].className ? Commented Oct 12, 2014 at 17:00
  • What is deckcard[0]...? Commented Oct 12, 2014 at 17:02
  • @BeNdErR - in the game will be 10 cards in the table, that would be 10 lines per card? 100 lines only to check if the class are the same? that's crazy, also would make the code "dirty". @T J - Those are cards taken from the deck then i put them in the table Commented Oct 12, 2014 at 17:09
  • I'd strongly suggest adding another class to each of the 'cards' (.card, for example), then creating the array with $('.card').get() (or [].slice.call(document.querySelectorAll('.card'), 0)). But, that aside, on what event do you want to compare these elements' classes? On click, DOMReady/load..? Commented Oct 12, 2014 at 17:13
  • @DavidThomas OnClick with a button made for that Commented Oct 12, 2014 at 17:16

2 Answers 2

1

The length attribute tells you how many elements are using that specific class:

var numOnes = $('.one').length;

So if numOnes is greater than 1 then you can assume that more than two elements are using that specific class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works perfectly, with some changes i did exactly what i wanted to, Many Thanks :).
0

You can do like this:

~(' '+tablecards[4].className+' ').indexOf(' '+<your class here>+' ');

This will tell if you have a class in that element or not.

This is one of the fastest ways to do this: no jQuery, no Regex, just a simple linear search.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.