1

I have an element which has two classes, and I want to get the class that wasn't used as a selector for this element. Exemple:

<div class="something anything"></div>

$(".something").attr("class");//Should return "anything"
$(".anything").attr("class");//Should return "something"

I am aware that .attr("class") returns "something anything", it was just for the exemple.

I have made searches on SO to find an answer, like this one, but I cannot use an index to find the "other" class, because the one I need to retrieve may be the first one, or the second one. My element will never have more than two classes.

All notices appreciated to help me handle this. Thanks.

Community
  • 1
  • 1
EdenSource
  • 3,387
  • 16
  • 29

5 Answers5

2

You will need to do it in 2 steps. 1) Get the class attribute 2) then replace the class used as selector.

You can do it like

function getOtherClass(clazz) {
  var classes = $('.' + clazz).attr('class');
  var regex = new RegExp('\\b' + clazz + '\\b');
  return classes.replace(regex, '');
}

snippet.log(getOtherClass('something'));
snippet.log(getOtherClass('anything'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<div class="something anything"></div>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

You can get it as below:

var result = $('.something').attr('class').split(' ');

for(var i =0; i < result.length; i++)
{
    if(result[i] != "something")
  {
      alert(result[i]);
     //// yu can get your value here.
  }

}

Demo: http://jsfiddle.net/Rj9bR/67/

Neel
  • 11,625
  • 3
  • 43
  • 61
1

I think this is the easiest way (if there are always two classes):

function getClass (clsName) {
  var classes = $("."+clsName).attr("class").split(" ");
  
  return (classes[0] == clsName) ? classes[1] : classes[0];
}

var cls = "something";
console.log(getClass(cls));

cls = "anything";
console.log(getClass(cls));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="something anything"></div>
Aleksei Golubev
  • 342
  • 3
  • 11
  • This solution is what I was expecting for, unfortunately, I can only accept one answer. Thanks for helping. – EdenSource Oct 01 '15 at 08:36
1

Define a method to ease your need,like

function GetClass(elemClass)
{
  var temp= $("." + elemClass).attr("class");
  temp=temp.Replace(elemClass,"").trim();
  return temp; //here you will have the classes you need
}

The above logic works great for 2 classes as your problem states but for many classes you just need to conver them to array and remove the given glass, it wont take much overhead from here to do so.

Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
  • This solution is what I was expecting for, unfortunately, I can only accept one answer. Thanks for helping. – EdenSource Oct 01 '15 at 08:31
1

Here is another easy solution

var myclass = $(".something").attr("class").replace("something","");
myclass = $.trim(myclass);  //remove empty space
  
  alert(myclass);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="something anything"></div>
Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52