Reputation: 855
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?
Upvotes: 0
Views: 344
Reputation: 4241
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.
Upvotes: 0
Reputation: 4967
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.
Upvotes: 1